【问题标题】:UITableView background filled only half?UITableView 背景只填了一半?
【发布时间】:2011-12-28 15:41:40
【问题描述】:

不幸的是,我用背景视图(渐变)填充了我的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;

渐变背景.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); 

【问题讨论】:

    标签: objective-c ios ipad uitableview background


    【解决方案1】:

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

    您还将 myView 的框架设置为 self.bounds 而不是 myTableView.bounds。

    【讨论】:

    • 试过这个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; 但我没有运气。
    【解决方案2】:

    知道了。我的渐变代码是错误的。我使用了视图的中间,但渐变应该在底部结束。

    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);
    

    【讨论】:

      猜你喜欢
      • 2015-03-10
      • 1970-01-01
      • 2016-05-28
      • 1970-01-01
      • 1970-01-01
      • 2014-12-01
      • 2015-06-07
      • 2011-12-14
      • 1970-01-01
      相关资源
      最近更新 更多