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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With