Can#39;t do custom UIButton in iOS6 with enabled storyboard autolayout(无法在 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法在 iOS6 中使用启用的故事板自动布局执行自定义 UIButton


基础教程推荐
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01