我在我的视图中添加了一个渐变图层,它在纵向模式下工作完美,因此无法在横向模式下工作。
func createGredientBackground() {
let layer = CAGradientLayer()
let center = UIColor(red: 249 / 255, green: 150 / 255, blue: 107 / 255, alpha: 1).CGColor
let end = UIColor(red: 247 / 255, green: 93 / 255, blue: 89 / 255, alpha: 1).CGColor
let start = UIColor(red: 255 / 255, green: 229 / 255, blue: 180 / 255, alpha: 1).CGColor
layer.colors = [start, center,end]
layer.frame = self.view.bounds //self.view.frame
print(layer.frame)
layer.startPoint = CGPointMake(0.0, 0.5)
layer.endPoint = CGPointMake(1.0, 0.5)
self.view.layer.insertSublayer(layer, atIndex: 0)
}
感谢。
答案 0 :(得分:9)
子图层不会自动调整大小以适合视图:当视图通过布局时,您应该更新它们:
func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
gradientLayer.frame = view.layer.bounds
}
没试过,所以也许你也应该尝试使用viewDidLayoutSubviews
。