How to Check If Device Orientation Is Landscape Left or Right in Swift

How to check if device orientation is landscape left or right in swift?

you can do something like,

if UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft{

}
else if UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeRight{

}
else if UIDevice.currentDevice().orientation == UIDeviceOrientation.UIDeviceOrientationPortraitUpsideDown{

}
else if UIDevice.currentDevice().orientation == UIDeviceOrientation.UIDeviceOrientationPortrait{

}

SWIFT 5

    if UIDevice.current.orientation.isLandscape {

} else if UIDevice.current.orientation.isFlat {

} else if UIDevice.current.orientation.isPortrait {

} else if UIDevice.current.orientation.isValidInterfaceOrientation {

}

SWIFT 3

if UIDevice.current.orientation == UIDeviceOrientation.landscapeLeft {

} else if UIDevice.current.orientation == UIDeviceOrientation.landscapeRight {

} else if UIDevice.current.orientation == UIDeviceOrientation.portrait {

} else if UIDevice.current.orientation == UIDeviceOrientation.portraitUpsideDown {

}

How to check if device orientation is landscape left vs right in flutter?

Flutter don't give us left or right yet but this library can help you.

iOS Landscape Left vs Right vs Wrong

I think mainly it's a matter of a contradiction between Xcode UI and info.plist. Xcode UI shows "Device Orientation", while info.plist speaks about "Supported Interface Orientation". But as we know those two are different things, so there's definitely something wrong there.

Assuming, between the two, that info.plist wins then those fields (for iPhone and iPad) are used to specify Supported Interface Orientation. I.e. the same option that you can also override in a specific view-controller through supportedInterfaceOrientations.

Instead the referenced doc page is about Device Orientation, with its own definition of what is landscapeLeft: home button to the right.

Now, looking at Interface Orientation Mask docs, there's no real detail on what is landscapeLeft, but old Xcode UI screenshots show that home button is to the left. See e.g. from this SO thread:

Sample Image

EDIT: Interface Orientation doc page instead has detail on what is landscapeLeft: home button to the left, as shown in old screenshots.

Conclusions

So, all in all, it seems to me that:

  1. Xcode UI/info.plist are about supported interface orientation, which has its own definition of landscape left/right

  2. Device orientation has the opposite definition of landscape left/right

How to check in which position (landscape or portrait) is the iPhone now?

Use the [[UIDevice currentDevice] orientation] method, as specified here.

How to detect orientation change?

Here's how I got it working:

In AppDelegate.swift inside the didFinishLaunchingWithOptions function I put:

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

and then inside the AppDelegate class I put the following function:

func rotated() {
if UIDeviceOrientationIsLandscape(UIDevice.current.orientation) {
print("Landscape")
}

if UIDeviceOrientationIsPortrait(UIDevice.current.orientation) {
print("Portrait")
}
}

In Swift, how to get the device orientation correctly right after it's launched?

I have tested many times about orientation, so I have summed up some experience.

In all iPhone devices, except iPhone6(s) plus, the only interface orientation is .portrait. If App is launched in landscape mode, there must be a change of orientation. One will receive the UIDeviceOrientationDidChangeNotification. It's an appropriate time to get the orientation.

Regarding the launching when in landscape with iPhone6, the orientation after the launch will change once:
Sample Image

The launching when in landscape with iPhone6 plus, after launch the orientation never changed:
Sample Image

Two different screenshot with the same app, Sample Image

So before the app does change orientation, the orientation is still like in the home page.

In viewDidLoad, the orientation has not changed yet, the log will be the wrong direction.

Lock the orientation of the app in a specific View Controller in Swift

You can force orientation with few steps:

Firstly, In your AppDelegate define a orientation property and conform supportedInterfaceOrientationsFor

var orientationLock = UIInterfaceOrientationMask.portrait

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return self.orientationLock
}

Then declare utility struct to set orientation using KVO:

struct AppOrientationUtility {
static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
if let delegate = UIApplication.shared.delegate as? AppDelegate {
delegate.orientationLock = orientation
}
}

static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation: UIInterfaceOrientation) {
self.lockOrientation(orientation)
UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
}
}

How to use:

//For portrait
AppOrientationUtility.lockOrientation(UIInterfaceOrientationMask.portrait, andRotateTo: UIInterfaceOrientation.portrait)

//For landscape
AppOrientationUtility.lockOrientation(UIInterfaceOrientationMask.landscapeRight, andRotateTo: UIInterfaceOrientation.landscapeRight)


Related Topics



Leave a reply



Submit