How to find out the current interface Orientation in the iOS 8 extension?

The InterfaceOrientation UIViewController is deprecated, and the document suggests using [[UIApplication sharedApplication] statusBarOrientation] , but there is no sharedApplication extension in the iOS 8 extension.


As @Retro pointed out, in most cases you can use self.traitCollection.verticalSizeClass or self.traitCollection.horizontalSizeClass in the UIViewController to get orientation information.

+7
ios ios8 custom-keyboard ios-app-extension
source share
4 answers

A UITraitCollection object provides information about the characteristics of the UIViewController object, which controls the set of views that make up your application interface. These characteristics or features determine the size class, display scale, and device idiom of the view controller. When a view controller is created, a collection of features is automatically created for this view controller.

You can create and modify a collection of views manager data to customize the application. The following methods create a new collection of characteristics containing only the passed parameter:

 traitCollectionWithDisplayScale: traitCollectionWithUserInterfaceIdiom: traitCollectionWithHorizontalSizeClass: traitCollectionWithVerticalSizeClass: 
+11
source share

Not outdated and will work with any device screen size (including future screen sizes that Apple will release this year).

 -(void)viewDidLayoutSubviews { NSLog(@"%@", self.view.frame.size.width == fminf([[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height) ? @"Portrait" : @"Landscape"); } 

Thanks to @anneblue for helping me shorten the code!

+3
source share

You can convert the borders of the screen to the keyboard coordinate system. After that, you can check that the width or height is greater.

-one
source share

Simple - just use:

[[UIApplication sharedApplication] statusBarOrientation]

-2
source share

All Articles