Objective c UITableView背景只填充了一半?
我用背景视图(渐变)填充了我的Objective c UITableView背景只填充了一半?,objective-c,ios,ipad,uitableview,background,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)]; myT
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);
您是否在myView上设置了自动ResizeMask?尝试将其设置为可变高度和宽度
您还将myView的框架设置为self.bounds,而不是myTableView.bounds。明白了。我的梯度代码是错误的。我使用了视图的中间部分,但是渐变应该在底部结束
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);
尝试此
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但是我运气不好。