本文介绍了iPhone 6s上放错了CAGradientLayer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些UILabels
带有彩色背景,并且向它们添加了CAGradientLayers
,如下所示.
I have a few UILabels
with colored background and I added CAGradientLayers
to them like below.
CAGradientLayer * gradientLayer = [CAGradientLayer layer];
gradientLayer.colors = [NSArray arrayWithObjects:
(id) [UIColor colorWithWhite:1 alpha:0.3].CGColor,
(id) [UIColor colorWithWhite:0.5 alpha:0.3].CGColor, nil];
gradientLayer.frame = label.bounds;
gradientLayer.cornerRadius = label.layer.cornerRadius;
[label.layer insertSublayer:gradientLayer atIndex:0];
在iPhone 4和iPhone 5s上看起来很完美,但是在iPhone 6s上CAGradientLayers
像下面的屏幕截图一样水平放置.
On iPhone 4 and iPhone 5s it looks perfect, but on iPhone 6s the CAGradientLayers
are misplaced horizontally like the following screenshot.
UIButton也会发生此问题.
This problem happens for UIButtons as well.
我正在运行iOS 9.3.
I am running iOS 9.3.
推荐答案
您需要的是处理自动布局约束.此快速代码表示您需要在viewDidLayoutSubviews
方法中执行的操作:
What you need is deal with autolayout constraints. This swift code represents what you need to do at viewDidLayoutSubviews
method:
class ViewController: UIViewController {
@IBOutlet weak var button: UIButton!
let gradientLayer = CAGradientLayer()
override func viewDidLoad() {
super.viewDidLoad()
gradientLayer.colors = [UIColor(white: 1, alpha: 0.3).CGColor,
UIColor(white: 0.5, alpha: 0.3).CGColor]
gradientLayer.frame = button.bounds
gradientLayer.cornerRadius = button.layer.cornerRadius
button.layer.insertSublayer(gradientLayer, atIndex: 0)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
gradientLayer.frame = button.bounds
}
}
这篇关于iPhone 6s上放错了CAGradientLayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!