iOS10 + 渐变停止工作 [英] iOS10 + Gradient stop working

查看:37
本文介绍了iOS10 + 渐变停止工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚刚移至Xcode8和iOS10,为其中一款应用提供对iOS10的支持.

Just been moved to Xcode8 and iOS10, providing support for iOS10 to one of the application.

找出奇怪的问题,突然梯度不起作用.

Figure out weird issue, suddenly Gradient is not working.

我发现在应用 Storyboard 之后(Xcode8 中的 Storyboard 更改)更改了视图不可见的渐变.

I found that after applying Storyboard (Storyboard changes in Xcode8) changes gradient not visible to views.

Xcode 7.3 - 渐变视图 -

Xcode 7.3 - Gradient Views -

Xcode 8 - 渐变视图 -

Xcode 8 - Gradient Views -

另外,如果我用旧的故事板替换故事板,它会显示渐变,所以新的故事板有问题.

Also, If I replace Storyboard with old storyboard it shows gradient, So there is something wrong with new Storyboard.

代码没有变化:

[view setOpaque:NO];
view.layer.cornerRadius = 45;
view.backgroundColor = [UIColor colorWithRed:23.0/255.0 green:29.0/255.0 blue:36.0/255.0 alpha:1.0];

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = view.bounds;

UIColor *color1 = [UIColor colorWithRed:33.0/255.0 green:39.0/255.0 blue:46.0/255.0 alpha:1.0];
UIColor *color2 = [UIColor colorWithRed:23.0/255.0 green:29.0/255.0 blue:36.0/255.0 alpha:1.0];
UIColor *color3 = [UIColor colorWithRed:14.0/255.0 green:20.0/255.0 blue:26.0/255.0 alpha:1.0];

NSArray *colorsArray = @[(id)color1.CGColor, (id)color2.CGColor, (id)color3.CGColor];

gradient.colors = colorsArray;
[gradient setLocations:@[@0.0, @0.35]];
[view.layer insertSublayer:gradient atIndex:0];

如果有人知道,请告诉我.

Let me know if any one figured it out.

我没有创建新的 Storyboard,我只是按照 Xcode8 的建议应用了以下更改

I didn't created new Storyboard, I just applied below changes as suggested by Xcode8

推荐答案

问题是布局的时机发生了变化.因此,您的代码现在运行得太快了.你在说

The problem is that there has been a change in the timing of layout. Therefore your code is now running too soon. You are saying

gradient.frame = view.bounds

但此时,视图的边界为零.所以梯度为零帧!这就是为什么你看不到它.您正在 viewDidLoad 中进行布局代码.这总是错误的,现在你明白为什么了.

But at this moment, view has zero bounds. So gradient has zero frame! That is why you cannot see it. You are doing layout code in viewDidLoad. This was always wrong and now you see why.

将您的代码移动到 viewDidLayoutSubviews.那时,视图有其真正的界限.您必须使用一个条件,以便您的代码只运行一次.

Move your code to viewDidLayoutSubviews. At that time, view has its real bounds. You will have to use a condition so that your code only runs once.

这篇关于iOS10 + 渐变停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆