无法在 iOS6 中使用启用的故事板自动布局执行自定义 UIButton

Can#39;t do custom UIButton in iOS6 with enabled storyboard autolayout(无法在 iOS6 中使用启用的故事板自动布局执行自定义 UIButton)
本文介绍了无法在 iOS6 中使用启用的故事板自动布局执行自定义 UIButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了奇怪的行为.我正在使用我在控制器中设置的自定义样式按钮:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)