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

后端 未结 4 706
孤街浪徒
孤街浪徒 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:14

    You needn't set the start and end point, given your goal is to have the gradient span the entire view. You're already setting that with `

    gradientLayer.frame = self.view.bounds
    

    `

    import UIKit
    class ViewController: UIViewController {
    
        @IBOutlet weak var myView: UIView!
        var gradientLayer: CAGradientLayer!
        override func viewDidLoad() {
            super.viewDidLoad()
        }
    
        override func viewWillAppear(animated: Bool) {
            super.viewWillAppear(animated)
            createGradientLayer()
        }
    
        func createGradientLayer() {
            gradientLayer = CAGradientLayer()
    
            gradientLayer.frame = self.view.bounds
    
            gradientLayer.colors = [UIColor.blueColor().CGColor, UIColor.whiteColor().CGColor]
    
            self.view.layer.addSublayer(gradientLayer)
        }
    }
    

提交回复
热议问题