【问题标题】:How to add a CAGradientLayer to a UIView when using programmatic constraints使用编程约束时如何将 CAGradientLayer 添加到 UIView
【发布时间】:2017-01-10 10:33:02
【问题描述】:

对于 Swift 中的 iOS 应用程序,我正在使用编程约束,并希望将 CAGradientLayer() 添加到 UIView()。下面是我的代码,它不起作用。

   import UIKit

   class ViewController: UIViewController {

       let animationView: UIView = {
           let view = UIView()
           view.translatesAutoresizingMaskIntoConstraints = false
           return view
       }()

       override func viewDidLoad() {
           super.viewDidLoad()

           view.addSubview(animationView)
           setupAnimationView()
           animationView.addGradientLayer()
       }

       func setupAnimationView() {
           animationView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
           animationView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
           animationView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
           animationView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
       }
   }

   extension UIView {

       func addGradientLayer() {
           let color1 = UIColor(red: 251/255, green: 55/255, blue: 91/255, alpha: 1.0)
           let color2 = UIColor(red: 1.0, green: 70/255, blue: 0, alpha: 1.0)
           let gradientLayer = CAGradientLayer()
           gradientLayer.name = "gradientLayer"
           gradientLayer.frame = self.frame
           gradientLayer.colors = [color1.cgColor, color2.cgColor]
           self.layer.insertSublayer(gradientLayer, at: 0)
       }
   }

我认为我遇到的问题与我使用编程约束创建动画视图有关,然后我尝试通过将其框架设置为等于框架视图来添加渐变层。当我不使用编程约束时,此代码有效。有什么想法我错过/做错了吗?

【问题讨论】:

  • 请把class关键字后面的冒号去掉
  • @Hamsternik 刚刚改了

标签: ios swift nslayoutconstraint cagradientlayer


【解决方案1】:

您添加到视图中的任何图层都不会在 AutoLayout 下进行管理,并且没有办法实现这一点。

您可以做的是将层存储为属性(可能在视图控制器上)。

然后在方法中...

override func viewDidLayoutSubviews() {
    super.viewDIdLayoutSubviews()
    // update the layers frame based on the frame of the view.
}

在这种方法中,视图将具有正确的框架,因此如果它已更改,您将需要相应地更新图层以具有新的框架。

【讨论】:

    【解决方案2】:

    您应该更改以下代码行:

    gradientLayer.frame = self.frame
    

    到:

    gradientLayer.frame = bounds
    

    这很重要,因为框架和边界没有使用相同的坐标空间。 frame是superview的坐标空间,bounds是view的内部坐标空间。

    您还可以在将您的 gradientLayer 添加到您的图层之前准确地告诉您渐变的起点和终点是什么:

    gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.5)
    gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.5)
    layer.addSublayer(gradientLayer)
    

    【讨论】:

      【解决方案3】:

      应立即调用layoutIfNeeded() 来更新视图。

      animationView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
      animationView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
      animationView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
      animationView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
      
      view.layoutIfNeeded()
      

      【讨论】:

      • 是的,我认为我在 viewDidLoad 中的订购有误 - 感谢您的帮助 @Matt
      • 这是关键,在任何图层操作之前调用view.layoutIfNeeded。
      猜你喜欢
      • 1970-01-01
      • 2015-11-30
      • 1970-01-01
      • 1970-01-01
      • 2016-08-25
      • 1970-01-01
      • 2017-04-22
      • 1970-01-01
      相关资源
      最近更新 更多