简体   繁体   English

在启用了故事板自动布局的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];
}

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). 它在iOS 5中运行正常。但是如果我在iOS 6中使用启用的Autolayout for Storyboard构建这个,那么我的风格中的渐变消失/变得透明(但标题仍然可见)。

If I'm disabling autolayout - gradient is back. 如果我禁用自动布局 - 渐变回来了。 Could somebody explain such behavior with autolayout? 有人可以用autolayout解释这种行为吗?

In viewDidLoad, under autolayout, your views will not yet have a frame, so you are making the layer have a frame of CGRectZero . 在viewDidLoad中,在autolayout下,您的视图还没有框架,因此您将使图层具有CGRectZero框架。

You need to move this code, or at least the part where you set the frame of the gradient layer, to viewDidLayoutSubviews or similar. 您需要将此代码或至少是设置渐变图层框架的部分移动到viewDidLayoutSubviews或类似代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM