Why gradient doesn't cover the whole width of the view

后端 未结 4 713
孤街浪徒
孤街浪徒 2021-01-05 19:44

I\'m trying to apply a gradient to a view which is constraint to the top, left and right of the main screen but for some reason the gradient doesn\'t cover the whole width o

4条回答
  •  遥遥无期
    2021-01-05 20:02

    You can simplify it by CustomClass with one line of code

    class GradientView: UIView {
       override class var layerClass: Swift.AnyClass {
          return CAGradientLayer.self
       }
    }
    
    
    // MARK: Usage
    
    @IBOutlet weak var myView: GradientView!
    
    guard let gradientLayer = myView.layer as? CAGradientLayer else { return }
    gradient.colors = [UIColor.blue.cgColor, UIColor.white.cgColor]
    gradient.startPoint = CGPoint(x: 0.0, y: 0.0)
    gradient.endPoint = CGPoint(x:0, y:0.6)
    

提交回复
热议问题