我对iOS开发很陌生,我正在尝试将我的视图背景设置为渐变。我创建了一个 CAGradientLayer 并尝试设置它。它通过构建,但我的应用程序一打开就崩溃,抛出“EXC_BAD_ACCESS”。这是我的代码:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
CAGradientLayer *bgLayer = [CAGradientLayer layer];
UIColor *grey = [UIColor colorWithRed:(255/255.0) green:(255/255.0) blue:(255/255.0) alpha:1.0];
UIColor *black = [UIColor colorWithRed:(180/255.0) green:(180/255.0) blue:(180/255.0) alpha:1.0];
NSNumber *top = [NSNumber numberWithFloat:0.0];
NSNumber *bot = [NSNumber numberWithFloat:1.0];
bgLayer.colors = [NSArray arrayWithObjects:grey, black, nil];
bgLayer.locations = [NSArray arrayWithObjects:top, bot, nil];
bgLayer.frame = self.view.bounds;
[self.view.layer addSublayer:bgLayer];
}
我想我明白它来自糟糕的内存管理,但不要我做错了什么。提前感谢您的回答。