如何在iOS 8中检测自定义键盘扩展中的方向更改?

在自定义键盘扩展中,我们不能使用

`didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation` 

sharedApplication

旋转时需要在键盘上检测纵向或横向。

如何检测自定义键盘扩展中的方向更改?

为了在方向更改时更新自定义键盘,请覆盖UIInputViewController中的UIInputViewController 。 据我所知,当发生旋转时,总是调用这个方法。

此外,由于传统的[UIApplication sharedApplication] statusBarOrientation]不起作用,要确定当前的方向,请使用以下片段:

 if([UIScreen mainScreen].bounds.size.width < [UIScreen mainScreen].bounds.size.height){ //Keyboard is in Portrait } else{ //Keyboard is in Landscape } 

希望这有助于!

不推荐使用,并且可以在任何设备屏幕尺寸上工作(包括苹果将​​在今年发布的未来屏幕尺寸)。

在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"); } 

完成。


要么………………

对于这个代码更易读的版本:

 -(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"); } } 

有一个简单的方法,只是看着屏幕宽度:

  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; 

使用viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:在您的viewController

 - (void)updateViewConstraints { [super updateViewConstraints]; // Add custom view sizing constraints here if (self.view.frame.size.width == 0 || self.view.frame.size.height == 0) return; [self.inputView removeConstraint:self.heightConstraint]; CGSize screenSize = [[UIScreen mainScreen] bounds].size; CGFloat screenH = screenSize.height; CGFloat screenW = screenSize.width; BOOL isLandscape = !(self.view.frame.size.width == (screenW*(screenW<screenH))+(screenH*(screenW>screenH))); NSLog(isLandscape ? @"Screen: Landscape" : @"Screen: Potriaint"); self.isLandscape = isLandscape; if (isLandscape) { self.heightConstraint.constant = self.landscapeHeight; [self.inputView addConstraint:self.heightConstraint]; } else { self.heightConstraint.constant = self.portraitHeight; [self.inputView addConstraint:self.heightConstraint]; } //trigger default first view [btn_gif sendActionsForControlEvents: UIControlEventTouchUpInside]; } 

willRotateToInterfaceOrientationdidRotateFromInterfaceOrientation可以使用,我只是在我的iPad上运行iOS 8.1上的键盘。 请注意,在iOS 8中已弃用这些willTransitionToTraitCollection ,但replace项willTransitionToTraitCollection可能不会被调用,因为旋转后的特征集合在键盘上不会更改。

在iOS 8.3之前,你必须使用

 -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 

这个不起作用(应该是个bug):

 -(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator 

在iOS 8.3及更高版本中,最好使用

 -(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator 

因为

 -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 

已弃用。

在某些情况下[UI​​Screen mainscreen] .bounds可能无法正常工作。 有时会在viewWillTransitionToSize之后更新:

尝试这个

 - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{ CGSize screenSize = [[UIScreen mainScreen] bounds].size; CGFloat realScreenHeight = MAX(screenSize.height, screenSize.width); if(size.width == realScreenHeight) NSLog(@"Landscape"); else NSLog(@"Portrait"); } 
  - (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]; }