我遇到了奇怪的行为.我正在使用我在控制器中设置的自定义样式按钮:
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 构建这个并启用了 Storyboard 的自动布局,那么我的样式中的渐变会消失/变得透明(但标题仍然可见).
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).
如果我禁用自动布局 - 渐变又回来了.有人可以用自动布局来解释这种行为吗?
If I'm disabling autolayout - gradient is back. Could somebody explain such behavior with autolayout?
在viewDidLoad中,在autolayout下,你的views还没有框架,所以你让layer有一个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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!