ios 中的键盘显示方向错误

Keyboard appears in wrong orientation in ios

我有一个 viewcontroller 支持横向和纵向的应用程序。

单击按钮时,会出现一个弹出窗口,我应该在其中输入名称。在纵向模式下一切正常。

但如果我关闭键盘,向左或向右旋转设备,然后打开弹出窗口,键盘仍会以纵向模式打开。

shouldAutorotate 返回 true 并且 supportedInterfaceOrientationsviewcontroller 中返回 AllButUpsideDown,因此旋转会自动发生。

我尝试了 this and this 个选项,但 none 个选项有效。

有什么想法吗?

尝试将以下代码添加到您的 viewcontroller 的 viewDidAppear 方法

 - (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
        [[UIDevice currentDevice] setValue:@(UIDeviceOrientationLandscapeLeft) forKey:@"orientation"];
        [[UIDevice currentDevice] setValue:@(self.interfaceOrientation) forKey:@"orientation"];

    }

好的,修复它,我猜是我的错。

似乎 Keyboard 和 UIViewController 分别调用 supportedInterfaceOrientations 并根据其 return 值旋转。我在那里有一个 if-else 声明,并且仅在某些情况下 returning AllButUpsideDown。当键盘检查它是否应该旋转方法 returned Portrait 时,对于 viewcontroller 值是 AllButUpsideDown.

所以我改变了这个:

public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations()
    {
        if (someStatement)
        {
            return UIInterfaceOrientationMask.AllButUpsideDown;
        }
        return UIInterfaceOrientationMask.Portrait;
    }

为此:

    public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations()
    {
        return UIInterfaceOrientationMask.AllButUpsideDown;
    }

现在只有 ShouldAutoRotate 决定是否轮换。

对某些人来说它应该是这样的:

public override bool ShouldAutorotate()
    {
        if (someStatement)
        {
            return true;
        }
        return false;
    }

    public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations()
    {
        return UIInterfaceOrientationMask.AllButUpsideDown;
    }

在我放弃对 iOS 8 的支持并将部署目标提高到 iOS 9 之后,我最近在我的一些视图控制器中得到了完全相同的错误键盘方向。结果是当基础 SDK 为 iOS 9 时,我以前的同事使用 解决了一个老问题(我们现在是 10,而从 Xcode 9 beta 编码时是 11)。该解决方案(基本上覆盖 UIAlertController 的 supportedInterfaceOrientations 以仅允许纵向)将强制使用更新的 SDK + 部署目标以纵向显示键盘,即使应用 window 和警报本身处于横向。

删除覆盖解决了问题,而且我没有看到任何警报过度警报的问题。

创建 UIAlertController 的子类

MyAlertController.h //header file
@interface MyAlertController : UIAlertController

@end

MyAlertController.m
@implementation MyAlertController 

- (BOOL)shouldAutorotate
{
  return NO;
}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
  [super supportedInterfaceOrientations];    
  return UIInterfaceOrientationMaskLandscape;
}
@end