Search code examples
ios8ios-app-extensioncustom-keyboarduiinputviewcontroller

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?


Solution

  • 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!