Reputation: 317
I know this question might look like a duplicate, however most answers disregard the fact of scrolling in the view.
When I scroll the gradient color stays constant through the whole scroll , and not entirely for the background. For example the height of the view is 1500, and the current view where i'm looking is (example) 450, then in this 450 the gradient is constant throughout the other 1050 instead of while scrolling it should get darker and darker (to the other color). Here is an image for better understanding
And this is the code I am using:
func setGradientColour() {
let colorTop = UIColor(red: 255.0/255.0, green: 149.0/255.0, blue: 0.0/255.0, alpha: 1.0).cgColor
let colorBottom = UIColor(red: 255.0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 1.0).cgColor
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [colorTop, colorBottom]
gradientLayer.locations = [0.0, 1.0]
gradientLayer.frame = self.view.bounds
self.view.layer.insertSublayer(gradientLayer, at: 0)
}
Which is being called in the viewDidLoad method.
Upvotes: 2
Views: 1797
Reputation: 317
Turns out the code was wrong and must be changed to:
func setGradientColour() {
let colorTop = UIColor(red: 255.0/255.0, green: 149.0/255.0, blue: 0.0/255.0, alpha: 1.0).cgColor
let colorBottom = UIColor(red: 255.0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 1.0).cgColor
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [colorTop, colorBottom]
gradientLayer.locations = [0.0, 1.0]
gradientLayer.frame = scrollViewTest.bounds
scrollViewTest.layer.insertSublayer(gradientLayer, at: 0)
}
Where scrollViewTest is the outlet for the scrollView. The mistake was that I was calling the normal view layer instead of the scrollview which reduced the length of view.
Upvotes: 1