在启用了故事板自动布局的iOS6中无法自定义UIButton [英] Can't do custom UIButton in iOS6 with enabled storyboard autolayout
问题描述
我遇到了奇怪的行为。我正在使用我在控制器中设置的自定义样式按钮:
I faced strange behavior. I'm using custom styled button which I setup in my controller:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.signOutButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.signOutButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
CAGradientLayer *btnGradient = [CAGradientLayer layer];
btnGradient.frame = self.signOutButton.bounds;
btnGradient.colors = [NSArray arrayWithObjects:
(id)[[UIColor colorWithRed:102.0f / 255.0f green:102.0f / 255.0f blue:102.0f / 255.0f alpha:1.0f] CGColor],
(id)[[UIColor colorWithRed:51.0f / 255.0f green:51.0f / 255.0f blue:51.0f / 255.0f alpha:1.0f] CGColor],
nil];
[self.signOutButton.layer insertSublayer:btnGradient atIndex:0];
}
它在iOS 5中运行正常但是如果我正在为iOS构建此功能6启用了故事板的Autolayout然后我的风格渐变消失/变得透明(但标题仍然可见)。
It works OK in iOS 5. But if I'm building this for iOS 6 with enabled Autolayout for Storyboard then gradient in my style disappears/becomes transparent (but title is still visible).
如果我禁用自动布局 - 渐变回来了。有人可以用autolayout解释这种行为吗?
If I'm disabling autolayout - gradient is back. Could somebody explain such behavior with autolayout?
推荐答案
在viewDidLoad中,在autolayout下,你的视图还没有框架,所以你使图层的框架为 CGRectZero
。
In viewDidLoad, under autolayout, your views will not yet have a frame, so you are making the layer have a frame of CGRectZero
.
您需要将此代码或至少是设置渐变图层框架的部分移动到 viewDidLayoutSubviews
或类似的。
You need to move this code, or at least the part where you set the frame of the gradient layer, to viewDidLayoutSubviews
or similar.
这篇关于在启用了故事板自动布局的iOS6中无法自定义UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!