2011-12-28 82 views
0

不幸的是,我填充了tableView背景視圖(漸變),只填充了背景的一半。UITableView背景只填充了一半?

任何想法如何填補下半年?

非常感謝提前!

代碼:

UIView* myView = [[GradientBackground alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, self.view.bounds.size.width, self.view.bounds.size.height)]; 

myTableView.backgroundColor = [UIColor clearColor]; 

myTableView.backgroundView = myView; 

Gradientbackground.m

- (id)initWithFrame:(CGRect)frame 
{ 
self = [super initWithFrame:frame]; 
if (self) { 
    // Initialization code 
} 
return self; 
} 

- (void)drawRect:(CGRect)rect 
{ 
CGContextRef currentContext = UIGraphicsGetCurrentContext();  
CGGradientRef glossGradient; 
CGColorSpaceRef rgbColorspace; 
size_t num_locations = 2; 
CGFloat locations[2] = { 0.0, 1.0 }; 
CGFloat components[8] = { 25.0/255.0, 89.0/255.0, 140.0/255.0, 0.65, // Start color 
25.0/255.0, 89.0/255.0, 140.0/255.0, 1.0 }; // End color 


//(25.0/255.0) green:(89.0/255.0) blue:(140.0/255.0) 

rgbColorspace = CGColorSpaceCreateDeviceRGB(); 
glossGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations); 

CGRect currentBounds = self.bounds; 
CGPoint topCenter = CGPointMake(CGRectGetMidX(currentBounds), 0.0f); 
CGPoint midCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMidY(currentBounds)); 
CGContextDrawLinearGradient(currentContext, glossGradient, topCenter, midCenter, 0); 

CGGradientRelease(glossGradient); 
CGColorSpaceRelease(rgbColorspace); 
} 

25.0/255.0, 89.0/255.0, 140.0/255.0, 1.0 }; // End color 


//(25.0/255.0) green:(89.0/255.0) blue:(140.0/255.0) 

rgbColorspace = CGColorSpaceCreateDeviceRGB(); 
glossGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations); 

CGRect currentBounds = self.bounds; 
CGPoint topCenter = CGPointMake(CGRectGetMidX(currentBounds), 0.0f); 
CGPoint midCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMidY(currentBounds)); 
CGContextDrawLinearGradient(currentContext, glossGradient, topCenter, midCenter, 0); 

CGGradientRelease(glossGradient); 
CGColorSpaceRelease(rgbColorspace); 

enter image description here

回答

1

明白了。我的梯度代碼是錯誤的。我使用了視圖的中間部分,但漸變應該在底部結束。

rgbColorspace = CGColorSpaceCreateDeviceRGB(); 
glossGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations); 

CGRect currentBounds = self.bounds; 
CGPoint topCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMaxY(currentBounds)); 
CGPoint bottomCent = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMinY(currentBounds)); 
CGContextDrawLinearGradient(currentContext, glossGradient, topCenter, bottomCent, 0); 

CGGradientRelease(glossGradient); 
CGColorSpaceRelease(rgbColorspace); 
1

你有設置MyView的的autoresizemask?嘗試將其設置爲可變的高度和寬度。

此外,您將myView的框架設置爲self.bounds而不是myTableView.bounds。

+0

嘗試這樣'的UIView * MyView的= [[GradientBackground的alloc] initWithFrame:方法CGRectMake(myTableView.bounds.origin.x,myTableView.bounds.origin.y,myTableView.bounds.size.width,myTableView.bounds。 size.height)]; myView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;'但我沒有運氣。 – Faser 2011-12-30 10:55:36