Get Device Current Orientation (App Extension)

How to get current orientation of device programmatically in iOS 6?

You can try this, may help you out:

How to change the device orientation programmatically in iOS 6

OR

Objective-c:

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]

Swift:

if UIDevice.current.orientation.isLandscape {
// Landscape mode
} else {
// Portrait mode
}

Different ways of getting current interface orientation?

self.interfaceOrientation returns UIInterfaceOrientation, current orientation of the interface. It is a property in UIViewController, you can access to this one only in UIViewController classes.

[[UIApplication sharedApplication] statusBarOrientation] returns UIInterfaceOrientation, current orientation of the application's status bar. You can access to that property in any point of your application. My experience shows that it's more effective way to retrieve real interface orientation.

[[UIDevice currentDevice] orientation] returns UIDeviceOrientation, device orientation. You can access to that property in any point of your application. But note that UIDeviceOrientation is not always UIInterfaceOrientation. For example, when your device is on a plain table you can receive unexpected value.

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.

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")
}
}

Hope this helps anyone else!

Thanks!

How to know current interfaceOrientation in extension of iOS 8?

A UITraitCollection object provides details about the characteristics of a UIViewController object, which manages a set of views that make up a portion of your app’s interface. These characteristics, or traits, define the size class, display scale, and device idiom of the view controller. When a view controller is created, a trait collection is automatically created for that view controller.

You can create and modify a view controller’s trait collection to customize your app. The following methods create a new trait collection containing only the passed parameter:

traitCollectionWithDisplayScale:

traitCollectionWithUserInterfaceIdiom:

traitCollectionWithHorizontalSizeClass:

traitCollectionWithVerticalSizeClass:


Related Topics



Leave a reply



Submit