iphone:将 CALayer 作为子层添加到 UIButton 的层会导致 EXC_BAD_ACCESS
嘿人们, 我尝试向 UIButtonTypeRoundedRect 添加一个漂亮的渐变。以下代码是在
viewWillAppear:(BOOL)animated
我的视图控制器的功能中实现的。
UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
tempButton.frame = CGRectMake(left, top, 80.0f, 80.0f);
CAGradientLayer * hintergrundButtonLayer = [[CAGradientLayer alloc] init];
[hintergrundButtonLayer setBounds:tempButton.bounds];
[hintergrundButtonLayer setColors:[NSArray arrayWithObjects:
[UIColor colorWithRed:0.2 green:0.3 blue:0.4 alpha:1.0],
[UIColor colorWithRed:0.4 green:0.5 blue:0.6 alpha:1.0],
nil]];
[hintergrundButtonLayer setPosition:
CGPointMake([tempButton bounds].size.width/2,
[tempButton bounds].size.height/2)];
[hintergrundButtonLayer setLocations:
[NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.0],
[NSNumber numberWithFloat:1.0],
nil]];
// hintergrundButtonLayer.zPosition = -10;
// hintergrundButtonLayer.frame = [tempButton bounds];
[tempButton addTarget:self action:@selector(switchVendorOnOff:) forControlEvents:UIControlEventTouchUpInside];
CALayer * downButtonLayer = [tempButton layer];
[downButtonLayer setMasksToBounds:YES];
[downButtonLayer setCornerRadius:10.0];
[downButtonLayer setBorderWidth:1.2];
[downButtonLayer setBorderColor:[[UIColor blackColor] CGColor]];
[downButtonLayer addSublayer:hintergrundButtonLayer];
[hintergrundButtonLayer release];
hintergrundButtonLayer = nil;
[self.view addSubview:tempButton];
代码运行良好,直到应用程序尝试显示视图 - 它因EXC_BAD_ACCESS而崩溃。但如果我注释掉该
[downButtonLayer addSublayer:hintergrundButtonLayer];
命令,那么一切都很好。
调试器没有告诉我任何有关泄漏的信息。分配模板也没有显示出什么奇怪的。除了 EXC_BAD_ACCESS 消息之外,控制台上没有打印任何其他内容。 “构建和分析”显示没有错误。
有人看到我的问题吗?
hey people,
I try to add a nice gradient to a UIButtonTypeRoundedRect. the following code is implemented in the
viewWillAppear:(BOOL)animated
function of my view controller.
UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
tempButton.frame = CGRectMake(left, top, 80.0f, 80.0f);
CAGradientLayer * hintergrundButtonLayer = [[CAGradientLayer alloc] init];
[hintergrundButtonLayer setBounds:tempButton.bounds];
[hintergrundButtonLayer setColors:[NSArray arrayWithObjects:
[UIColor colorWithRed:0.2 green:0.3 blue:0.4 alpha:1.0],
[UIColor colorWithRed:0.4 green:0.5 blue:0.6 alpha:1.0],
nil]];
[hintergrundButtonLayer setPosition:
CGPointMake([tempButton bounds].size.width/2,
[tempButton bounds].size.height/2)];
[hintergrundButtonLayer setLocations:
[NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.0],
[NSNumber numberWithFloat:1.0],
nil]];
// hintergrundButtonLayer.zPosition = -10;
// hintergrundButtonLayer.frame = [tempButton bounds];
[tempButton addTarget:self action:@selector(switchVendorOnOff:) forControlEvents:UIControlEventTouchUpInside];
CALayer * downButtonLayer = [tempButton layer];
[downButtonLayer setMasksToBounds:YES];
[downButtonLayer setCornerRadius:10.0];
[downButtonLayer setBorderWidth:1.2];
[downButtonLayer setBorderColor:[[UIColor blackColor] CGColor]];
[downButtonLayer addSublayer:hintergrundButtonLayer];
[hintergrundButtonLayer release];
hintergrundButtonLayer = nil;
[self.view addSubview:tempButton];
the code runs fine until the app tries to display the view - it crashs with a EXC_BAD_ACCESS. But if I comment out the
[downButtonLayer addSublayer:hintergrundButtonLayer];
command, then everything is fine.
The debugger tells me nothing about leaks. Also the allocation template shows nothing strange. There is nothing else printed to the console than the EXC_BAD_ACCESS message. The "build and analyse" shows no errors.
Does anyone see my problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
setColors 采用 CGColors,而不是 UIColors。
如果您在崩溃中读取顶部堆栈帧(在调试器下运行时),它们表明 CGColors 存在问题:
setColors takes CGColors, not UIColors.
If you read the top stack frames in the crash (while running under the debugger), they suggest a problem with CGColors: