CALayer not resizing with Autolayout

后端 未结 2 1627
南方客
南方客 2020-12-14 09:14

I have created a progress bar to be used in a tableView by creating a gradient layer. It works perfectly.

iPhone5:

2条回答
  •  天涯浪人
    2020-12-14 09:36

    As an alternative to the accepted answer, you could also change the views layer class to be CAGradientLayer. The views layer will always be resized according to layout changes. You can achieve that by subclassing UIView

    class GradientView: UIView {
      override class func layerClass() -> AnyClass {
        return CAGradientLayer.self
      }
    }
    

    then set the colors

    if let gradientLayer = gradientView.layer as? CAGradientLayer {
      gradientLayer.colors = arrayColors
    }
    

    It's less code than adding and maintaining a sublayer, but might not suit all use cases.

提交回复
热议问题