Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradient does not fill all the UIView

I want to create a gradient layer and insert it in a view but the view is not fully filled...

My actual code is :

    gradientLayer.frame = topView.bounds
    gradientLayer.colors = [UIColor.white.cgColor, UIColor.black.cgColor]

    topView.layer.insertSublayer(gradientLayer, at: 0)

This is my code and storyboard (In red is the part I want to fill)

This is my result...

I don't have clue on it.. It works on fullscreen view but not in more little view..

The top view is imported from the storyboard of course. Maybe I did a mistake with view.bound I don't know. I'm a beginner in swift and iOS so everything is possible !

EDIT : The problem is not the size of the view but the size of the layer which is too small because the background color of the view fill all the space but not the layer

like image 585
Psyycker Avatar asked Dec 05 '22 13:12

Psyycker


1 Answers

This has to do with the constraints in your storyboard not being applied until the subviews are laid out. At viewDidLoad() the view has a fixed size from the storyboard. If you move the code that sets the layer's frame to viewDidLayoutSubviews() the constraints have been applied to topView and it then has the correct frame. You can then set the layer's frame.

Add this to your ProfileController

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    gradientLayer.frame = topView.frame
}

Then it will fill the whole view.

like image 122
Nordeast Avatar answered Dec 20 '22 19:12

Nordeast