Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 218
Fire Fist Avatar asked Jun 11 '14 15:06

Fire Fist


People also ask

How do I customize my keyboard IOS?

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.

How do I get the IOS keyboard extension?

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.

How do you handle orientation in react native?

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.


2 Answers

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!

like image 166
Matt Avatar answered Sep 22 '22 06:09

Matt


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");     } } 
like image 39
Albert Renshaw Avatar answered Sep 19 '22 06:09

Albert Renshaw