-4

I'm creating an iPhone game in Xcode with the latest version of Swift. Why I'm asking this question is when the user flips the phone and changes the orientation, is there any way I can detect that, so I can change the placing, size, etc. of things?

CoolUserName
  • 137
  • 1
  • 16

1 Answers1

4

You may want to look into constraints so that your game looks good in both portrait and landscape if that is what you are wanting to support. You could set your constraints in storyboard then press vary for traits and choose the landscape option to set your new constraints for landscape.

Else, if you really needed to check orientation changes you could try:

override func viewDidLoad() {
    super.viewDidLoad()        

    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.rotated), name: UIDevice.orientationDidChangeNotification, object: nil)
}

deinit {
   NotificationCenter.default.removeObserver(self, name: UIDevice.orientationDidChangeNotification, object: nil)         
}

func rotated() {
    if UIDevice.current.orientation.isLandscape {
        print("Landscape")
    } else {
        print("Portrait")
    }
}

Source

DrewG23
  • 427
  • 3
  • 11