简体   繁体   English

iOS 8.3键盘方向错误

[英]iOS 8.3 keyboard orientation bug

I am facing a strange iOS 8.3 issue which shows a keyboard on a wrong orientation like this (the view controller is in Landscape mode, but the keyboard show up in Portrait mode): 我正面临一个奇怪的iOS 8.3问题,它显示键盘的方向错误(视图控制器处于横向模式,但键盘显示在纵向模式):

视图控制器处于横向模式,但键盘以纵向模式显示

I can trigger this issue by following these steps: 我可以通过以下步骤触发此问题:

  1. Create 2 UIViewController subClass: ViewControllerA and ViewControllerB 创建2个UIViewControllerViewControllerAViewControllerB

  2. in ViewControllerA implement supportedInterfaceOrientations and return UIInterfaceOrientationMaskPortrait ViewControllerA实现supportedInterfaceOrientations并返回UIInterfaceOrientationMaskPortrait

  3. in ViewControllerB implement supportedInterfaceOrientations and return UIInterfaceOrientationMaskLandscape ViewControllerB实现supportedInterfaceOrientations并返回UIInterfaceOrientationMaskLandscape

  4. Create a UINavigationController subclass called NavigationController , implement supportedInterfaceOrientations and return [self.topViewController supportedInterfaceOrientations] (I'm doing this because I want to keep the NavigationController and it's rootVC from rotating) 创建一个名为NavigationControllerUINavigationController子类,实现supportedInterfaceOrientations并返回[self.topViewController supportedInterfaceOrientations] (我这样做是因为我想保持NavigationController并且它的rootVC不能旋转)

  5. Use the NavigationController as initial view controller of the app, set ViewControllerA as the NavigationController 's rootViewContrller 使用NavigationController作为应用程序的初始视图控制器,将ViewControllerA设置为NavigationControllerrootViewContrller

  6. Launch the app, ViewControllerA will shown up in Portrait. 启动应用程序, ViewControllerA将显示在Portrait中。 Show a button on ViewControllerA , press the button will present ViewControllerB by using presentViewController:animated:completion ViewControllerA上显示一个按钮,按下按钮将通过使用presentViewController:animated:completion呈现ViewControllerB

  7. ViewControllerB will show up in Landscape; ViewControllerB将出现在Landscape中; Show a text field on ViewControllerB , tap on the text field will trigger the keyboard, but the keyboard is in Portrait mode, just like the image above. ViewControllerB上显示文本字段,点击文本字段将触发键盘,但键盘处于纵向模式,就像上面的图像一样。

PS. PS。 You can download and run the Xcode project on github 您可以在github上下载并运行Xcode项目

This issue seems only appears on iOS 8.3. 此问题似乎只出现在iOS 8.3上。 Am I doing something wrong ? 难道我做错了什么 ? Or maybe this is just another bug of iOS ? 或许这只是iOS的另一个错误?

By the way, this issue won't happen if you just show ViewControllerA directly without a ViewController . 顺便说一句,如果你只是在没有ViewController情况下直接显示ViewControllerA ,就不会发生这个问题。 So if this is a bug of iOS, how can I avoid subclassing UINavigationController but still keep ViewControllerA which is the rootViewController of a UINavigationController from rotating. 因此,如果这是iOS的错误,我怎么能避免继承UINavigationController但仍然保持ViewControllerAUINavigationController的rootViewController)旋转。


UPDATE: This bug still appears on iOS 8.4, I fired a bug report and got replies from apple on June 17th 2015, they said it has been addressed in the latest iOS 9 beta. 更新:此错误仍然出现在iOS 8.4上,我在2015年6月17日发布了一个错误报告并得到苹果的回复,他们表示已经在最新的iOS 9测试版中解决了这个问题。

I've asked a tech support and they said 我问过技术支持,他们说

Our engineers have reviewed your request and have determined that this would be best handled as a bug report. 我们的工程师已经审核了您的请求,并确定最好将其作为错误报告处理。

So seems that it's confirmed a system bug. 所以似乎证实了系统错误。

And my current solution is adding a ViewControllerC (support both portrait and landscape mode) between ViewControllerA and ViewControllerB: ViewControllerA should present ViewControllerC with animation, after ViewControllerC appeared, present ViewControllerB without animation. 我目前的解决方案是在ViewControllerA和ViewControllerB之间添加一个ViewControllerC(支持纵向和横向模式):ViewControllerA应该向ViewControllerC提供动画,在ViewControllerC出现后,呈现ViewControllerB而不带动画。

To make the transition looks natural, you can set the same background colour as ViewControllerB to ViewControllerC, or take a screen shot of ViewControllerB and add it to ViewControllerC as a background image. 要使转换看起来自然,您可以将相同的背景颜色设置为ViewControllerB到ViewControllerC,或者拍摄ViewControllerB的屏幕截图并将其作为背景图像添加到ViewControllerC。

It's not a perfect solution, for users, they may find out ViewControllerB takes more time to load. 对于用户而言,它不是一个完美的解决方案,他们可能会发现ViewControllerB需要更多时间来加载。

We got around this issue by tricking the device into thinking that it was rotated into a different orientation and then into its intended orientation. 我们通过欺骗设备认为它被旋转到不同的方向然后进入其预期的方向来解决这个问题。

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.segmentedControl.selectedSegmentIndex = 0;
    if(!self.rotatingForDismissal)
    {
        [self.tableNumberTextField becomeFirstResponder];
    }


    if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.3"))
    {
        [[UIDevice currentDevice] setValue:@(UIDeviceOrientationLandscapeLeft) forKey:@"orientation"];
        [[UIDevice currentDevice] setValue:@(self.interfaceOrientation) forKey:@"orientation"];
    }

}

I was able to make the keyboard appear correctly using the following. 我能够使用以下内容使键盘正确显示。

- (void)viewDidAppear:(BOOL)animated
{
  [super viewDidAppear:animated];

  if (SYSTEM_VERSION_GREATER_THAN(@"7.1") && SYSTEM_VERSION_LESS_THAN(@"9.0"))
  {
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    [[UIDevice currentDevice] setValue:@(UIDeviceOrientationUnknown) forKey:@"orientation"];
    [[UIDevice currentDevice] setValue:@(orientation) forKey:@"orientation"];
  }
}

Note that using UIDeviceOrientationUnknown does not cause side effects such as inappropriate auto rotation of views. 请注意,使用UIDeviceOrientationUnknown不会导致副作用,例如不适当的视图自动旋转。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM