user3766930 user3766930 - 1 year ago 69
Swift Question

my view looks different on iphone 5 and iphone 6 even though it has constraints - what is wrong here?

In my

app I am applying gradient to my view. I do it with the following code:

@IBOutlet weak var gradientView: UIView!

override func viewDidLoad(){

let layer = CAGradientLayer()
layer.frame = CGRect(x: 0, y: 0, width: self.gradientView.frame.width, height: self.gradientView.frame.height)
layer.colors = [UIColor.clear.cgColor, UIColor.white.cgColor]

in my storyboard the
is the black one here:

enter image description here

its constraints are:

enter image description here

so it touches edges on each side, the top and the white panel on the bottom.

When I run it in iphone5 simulator I have:

enter image description here

and that's fine. But when I run it on iphone 6s, I see:

enter image description here

What's the problem here, since the constraints are applied?

Answer Source

The problem is that your gradientView's frame is not updated yet in viewDidLoad. Try this instead:

let gradientLayer = CAGradientLayer() 

override func viewDidLoad(){
    gradientLayer.colors = [UIColor.clear.cgColor, UIColor.white.cgColor]

override func viewDidLayoutSubviews(){
    gradientLayer.frame = gradientView.bounds
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download