我想创建一个渐变图层并将其插入 View 中,但 View 未完全填充...
我的实际代码是:
gradientLayer.frame = topView.bounds
gradientLayer.colors = [UIColor.white.cgColor, UIColor.black.cgColor]
topView.layer.insertSublayer(gradientLayer, at: 0)
This is my code and storyboard (In red is the part I want to fill)
This is my result...
我没有任何提示。.它可以在全屏 View 下使用,但不能在更小的 View 上使用..
当然,顶 View 是从 Storyboard 中导入的。也许我在view.bound上弄错了我不知道。我是Swift和iOS的初学者,所以一切皆有可能!
编辑 :
问题不是 View 的大小,而是图层的大小,这太小了,因为 View 的背景色填充了所有空间,但不是整个图层
最佳答案
这与布局 subview 之前不会应用 Storyboard 中的约束有关。在viewDidLoad()
处, View 在 Storyboard 中具有固定大小。如果移动将图层的框架设置为viewDidLayoutSubviews()
的代码,则约束已应用于topView
,然后它具有正确的框架。然后,您可以设置图层的框架。
将此添加到您的ProfileController
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
gradientLayer.frame = topView.frame
}
然后它将填充整个 View 。