iOS10 + Gradient stop working [duplicate]

梦想与她 提交于 2019-12-12 04:31:56

问题


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.

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

Xcode 7.3 - Gradient Views -

Xcode 8 - Gradient Views -

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

There is no change in code:

[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.

Edit:

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


回答1:


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

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.

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.



来源:https://stackoverflow.com/questions/40151827/ios10-gradient-stop-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!