UITableView背景只填充了一半?

时间:2011-12-28 15:41:41

标签: objective-c ios ipad uitableview background

不幸的是,我用tableView填充了背景视图(渐变),只填充了一半的背景。

任何想法如何填补下半场?

提前非常感谢!

代码:

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

2 个答案:

答案 0 :(得分:1)

您是否在myView上设置了autoresizingmask?尝试将其设置为可变的高度和宽度。

此外,您将myView的框架设置为self.bounds而不是TableView.bounds。

答案 1 :(得分: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);