Swift - How to detect orientation changes

后端 未结 13 1815
余生分开走
余生分开走 2020-11-29 19:49

I want to add two images to single image view (i.e for landscape one image and for portrait another image)but i don\'t know how to detect orientation changes using swift lan

13条回答
  •  借酒劲吻你
    2020-11-29 20:41

    let const = "Background" //image name
    let const2 = "GreyBackground" // image name
        @IBOutlet weak var imageView: UIImageView!
        override func viewDidLoad() {
            super.viewDidLoad()
    
            imageView.image = UIImage(named: const)
            // Do any additional setup after loading the view.
        }
    
        override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
            super.viewWillTransition(to: size, with: coordinator)
            if UIDevice.current.orientation.isLandscape {
                print("Landscape")
                imageView.image = UIImage(named: const2)
            } else {
                print("Portrait")
                imageView.image = UIImage(named: const)
            }
        }
    

提交回复
热议问题