Henry
Henry

Reputation: 479

How to know current interfaceOrientation in extension of iOS 8?

InterfaceOrientation of UIViewController is deprecated and the Document suggest to use [[UIApplication sharedApplication] statusBarOrientation], but there is no sharedApplication in an extension of iOS 8.


As @Retro mentioned, in most circumstances, you can use self.traitCollection.verticalSizeClass or self.traitCollection.horizontalSizeClass in a UIViewController to get orientation information.

Upvotes: 7

Views: 9681

Answers (4)

Retro
Retro

Reputation: 4005

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:

Upvotes: 11

Albert Renshaw
Albert Renshaw

Reputation: 17892

Non-deprecated and will work on any device screen size (including future screen sizes Apple will be releasing this year).

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

Thanks @anneblue for helping shorten the code!

Upvotes: 3

cetcet
cetcet

Reputation: 2130

Simple - just use:

[[UIApplication sharedApplication] statusBarOrientation]

Upvotes: -3

Serg
Serg

Reputation: 41

You can convert screen bounds to keyboard view's coordinate system. After that you can check what is bigger width or height.

Upvotes: -1

Related Questions