How to Detect Orientation in an App Extension

How to detect Orientation Change in Custom Keyboard Extension in iOS 8?

In order to update your custom keyboard when the orientation changes, override viewDidLayoutSubviews in the UIInputViewController. As far as I can tell, when a rotation occurs this method is always called.

Additionally, as the traditional [UIApplication sharedApplication] statusBarOrientation] doesn't work, to determine the current orientation use the following snippet:

if([UIScreen mainScreen].bounds.size.width < [UIScreen mainScreen].bounds.size.height){
//Keyboard is in Portrait
}
else{
//Keyboard is in Landscape
}

Hopefully this helps!

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

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