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

后端 未结 10 1875

In Custom Keyboard Extension , we can\'t use

`didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation` 

and

相关标签:
10条回答
  • 2020-12-04 18:31
          - (void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator 
        {   
         [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) 
            {      UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];      // do whatever 
              } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {    }];       [super viewWillTransitionToSize: size withTransitionCoordinator: coordinator]; 
     }
    
    0 讨论(0)
  • 2020-12-04 18:32

    use viewWillTransitionToSize:(CGSize)size withTransitionCoordinator: inside your viewController

    0 讨论(0)
  • 2020-12-04 18:38

    There is a simple way, just looking at the screen width:

     double width = [[UIScreen mainScreen] bounds].size.width;
     double interfaceWidth = MIN([[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);
    
     BOOL isPortrait = (width == interfaceWidth) ? YES : NO;
    
    0 讨论(0)
  • 2020-12-04 18:39

    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!

    0 讨论(0)
提交回复
热议问题