How to add a full-screen gradient background in an iOS app using Swift?
Hi everyone! I'm a beginner in iOS development with Swift and I've run into a problem. I want to create a beautiful gradient background for the entire screen in my app. I've heard about CAGradientLayer, but I can't figure out how to properly apply it to the controller's background view.
Here's what I tried:
```swift
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [UIColor.blue.cgColor, UIColor.purple.cgColor]
gradientLayer.locations = [0.0, 1.0]
gradientLayer.frame = self.view.bounds
self.view.layer.addSublayer(gradientLayer)
}
}
```
But the gradient displays incorrectly — it doesn't fill the entire screen, especially when rotating
No answers yet.
Log in to leave an answer