Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting device orientation in Swift

I was wondering how I can get the current device orientation in Swift? I know there are examples for Objective-C, however I haven't been able to get it working in Swift.

I am trying to get the device orientation and put that into an if statement.

This is the line that I am having the most issues with:

[[UIApplication sharedApplication] statusBarOrientation] 
like image 725
user3746428 Avatar asked Sep 11 '14 20:09

user3746428


People also ask

How do I get device orientation in Swift?

The value of the property is a constant that indicates the current orientation of the device. This value represents the physical orientation of the device and may be different from the current orientation of your application's user interface. See “UIDeviceOrientation” for descriptions of the possible values.

How do I get orientation on iOS?

If you want to get device orientation directly from accelerometer use [[UIDevice currentDevice] orientation] . But if you need current orientation of your application(interface orientation) use [[UIApplication sharedApplication] statusBarOrientation] .

What is device orientation in Xcode?

Upside Down: This orientation means you change the orientation of your iOS device 180 degrees. But if you check this checkbox only, you will get the below error message when you run the app. So this checkbox should be used with other orientations such as Portrait or Landscape.


1 Answers

you can use:

override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {     var text=""     switch UIDevice.currentDevice().orientation{     case .Portrait:         text="Portrait"     case .PortraitUpsideDown:         text="PortraitUpsideDown"     case .LandscapeLeft:         text="LandscapeLeft"     case .LandscapeRight:         text="LandscapeRight"     default:         text="Another"     }     NSLog("You have moved: \(text)")         } 

SWIFT 3 UPDATE

override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {     var text=""     switch UIDevice.current.orientation{     case .portrait:         text="Portrait"     case .portraitUpsideDown:         text="PortraitUpsideDown"     case .landscapeLeft:         text="LandscapeLeft"     case .landscapeRight:         text="LandscapeRight"     default:         text="Another"     }     NSLog("You have moved: \(text)")         } 

or

override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) { } 

with Notification you can check: IOS8 Swift: How to detect orientation change?

NOTE : didRotateFromInterfaceOrientation is Deprecated Use viewWillTransitionToSize for iOS 2.0 and later

In case of Face up and Face Down this will not work. So we need to use the following.

if UIApplication.shared.statusBarOrientation.isLandscape {      // activate landscape changes } else {      // activate portrait changes } 
like image 50
Miguel Avatar answered Sep 23 '22 02:09

Miguel