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
Allow "Auto Lock" While Video Is Being Played
How to Emulate Nfc Cards in iOS 13
Getting System Uptime in iOS/Swift
iOS 13 Set Uisearchtextfield Placeholder Color
How to Determine File Size on Disk of a Video Phasset in iOS8
Mask Uiview with Uibezierpath - Stroke Only
Add Button at the End of Collection View in Storyboard
How to Monitor Battery Level and State Changes Using Swift
Getting Location for an iOS App When It Is in the Background and Even Killed
How Can One Enable Keyboard Like in Imessages/Fb Messenger in Landscape Mode at iOS8
Rounding Uiimage and Adding a Border
Error Using Swift - Instance Member Cannot Be Used on Type 'Viewcontroller'
Uilabel, Uifont and Utf-8 Triangle
All Notifications Disappearing After Opening One of Them
Page Based "Reloadrootcontrollerswithnames:" on Launch Loop
Having Uiview Drawrect Occur in a Background Thread
How to Use Avcapturephotooutput to Capture Photo Swift + Xcode