UITableView的渐变背景

Cry*_*tal 4 iphone uitableview

我在使用一些顾问代码在UITableView上创建一个简单的渐变时遇到了麻烦.单击按钮时,会显示一个弹出窗口.在popover中,我有一个navigationController,它的rootViewController是我的自定义类.我的班级有这些属性

@interface TargetDetailView : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
    CGGradientRef backgroundGradient;
    UITableView *_targetTableView;
}

@property (retain, nonatomic) IBOutlet UIGradientView *backgroundView;
@property (nonatomic, retain) IBOutlet UITableView *TargetTableView;
Run Code Online (Sandbox Code Playgroud)

在viewDidLoad中,我这样做:

CGColorSpaceRef gray = CGColorSpaceCreateDeviceGray();
    backgroundGradient = CGGradientCreateWithColors(gray, (CFArrayRef)[NSArray arrayWithObjects:(id)[[UIColor colorWithWhite:70./255 alpha:1] CGColor], (id)[[UIColor colorWithWhite:35./255 alpha:1] CGColor], nil], nil);
    CGColorSpaceRelease(gray);
    [self.backgroundView setGradient:backgroundGradient];
Run Code Online (Sandbox Code Playgroud)

我的单元格backgroundView设置为[UIColor clearColor].此外,我在IB中的tableView也设置了背景[UIColor clearColor].然而,当我呈现我的弹出窗口时,我看不到渐变.在IB中,层次结构是类的.view属性,然后是backgroundView出口,以及该视图中的UITableView.

我已经尝试过其他一些问题来排除故障,比如完全摆脱gradientView,并将tableView作为类的.view属性中唯一的项目.然后我尝试self.backgroundColor = [UIColor orangeColor];

而且我没有看到任何颜色.一切看起来都是灰色的.我究竟做错了什么?谢谢.

Mil*_*deh 7

这是我用于UILable渐变的代码,对于所有UIViews都是一样的:

gradient = [CAGradientLayer layer];
gradient.frame = lbl_change.bounds;

//Blue Color
gradient.colors = [NSArray arrayWithObjects:(id)[UIColorFromRGB(0x2B60DE) CGColor],    (id)[UIColorFromRGB(0x2554C7) CGColor], nil];

[lbl_change.layer insertSublayer:gradient atIndex:0];
Run Code Online (Sandbox Code Playgroud)

我已将渐变添加到图层,并确保您已为渐变设置了框架.

  • 我这样做是为了克服问题`UIView*tableBackgroundView = [[UIView alloc] init]; [tableBackgroundView.layer insertSublayer:gLayer atIndex:1]; [tableView setBackgroundView:tableBackgroundView];`..现在它工作正常;) (7认同)