In Custom Keyboard Extension , we can't use
`didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation`
and sharedApplication
.
I need to detect portrait or landscape in keyboard when rotate.
How can i detect when orientation change in Custom Keyboard extension?
Navigate to Settings > General > Keyboard > Keyboards > Add New Keyboard and select AC Custom Keyboard. This will add it to the list of available keyboards. Go back to your app and bring up the keyboard by tapping the text view. Tap and hold the globe key and select AC Keyboard from the list that pops up.
Add a Custom Keyboard Target to Your App Xcode configures your project to build the extension and includes it in your app's bundle. Open your app project in Xcode. Choose File > New > Target. Select Custom Keyboard Extension from the Application Extension group.
Screen orientation lock For Android, open the AndroidManifest. xml file and within the activity element add 'android:screenOrientation="portrait"' to lock to portrait or 'android:screenOrientation="landscape"' to lock to landscape.
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!
Non-deprecated and will work on any device screen size (including future screen sizes Apple will be releasing this year).
In CustomKeyboard ViewController.m
:
-(void)viewDidLayoutSubviews { NSLog(@"%@", (self.view.frame.size.width == ([[UIScreen mainScreen] bounds].size.width*([[UIScreen mainScreen] bounds].size.width<[[UIScreen mainScreen] bounds].size.height))+([[UIScreen mainScreen] bounds].size.height*([[UIScreen mainScreen] bounds].size.width>[[UIScreen mainScreen] bounds].size.height))) ? @"Portrait" : @"Landscape"); }
done.
Or... for a more easy to read version of this code:
-(void)viewDidLayoutSubviews { int appExtensionWidth = (int)round(self.view.frame.size.width); int possibleScreenWidthValue1 = (int)round([[UIScreen mainScreen] bounds].size.width); int possibleScreenWidthValue2 = (int)round([[UIScreen mainScreen] bounds].size.height); int screenWidthValue; if (possibleScreenWidthValue1 < possibleScreenWidthValue2) { screenWidthValue = possibleScreenWidthValue1; } else { screenWidthValue = possibleScreenWidthValue2; } if (appExtensionWidth == screenWidthValue) { NSLog(@"portrait"); } else { NSLog(@"landscape"); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With