Source: SVG-Test
See: https://dzone.com/articles/calayer-and-auto-layout-with-swift-1
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var imgBackground: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
setBackgroundImage()
view.layer.addSublayer(gradientLayer)
gradientLayer.frame = view.bounds
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
// super.viewWillTransition(to: size, with: coordinator)
// print ("Orientation changed")
// setBackgroundImage()
// self.imgBackground.layer.frame = self.view.bounds
// }
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
print ("Orientation changed")
setBackgroundImage()
gradientLayer.frame = view.bounds
}
func setBackgroundImage() {
print ("setBackgroundImage")
let orientation = UIDevice.current.orientation
if orientation.isLandscape {
imgBackground.image = UIImage(named: "orange-apple-2732x2732l.png")
print ("Landscape")
} else {
imgBackground.image = UIImage(named: "orange-apple-2732x2732p.png")
print("Portrait")
}
}
let gradientLayer: CAGradientLayer = {
let layer = CAGradientLayer()
layer.colors = [
UIColor.clear.cgColor,
UIColor.blue.cgColor
]
return layer
}()
}