在 iOS 9 中强制 UIViewController 只是横向

Force a UIViewController just landscape in iOS 9

我有一个应用程序支持纵向和横向。但是在 viewcontroller 中,我只希望它以横向模式显示。我尝试覆盖一些改变方向的方法,例如

- (BOOL)shouldAutorotate {
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return (UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight);
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
        return YES;
    }
    return NO;
}

但是在iOS9中调用了none个。因此,这个viewcontroller同时呈现纵向和横向。相反,它在 iOS 8.

中完美运行

太烦人了,有没有办法在 iOS 9

中强制仅以横向模式显示 viewcontroller

更新: 作为 Ronak Chaniyara 的回答,我解决了我的问题,只有一个控制器处于横向模式。 现在我面临另一个问题。如果我想在纵向模式下强制一个控制器,我在控制器中实现了这些方法,但如果我旋转屏幕,它仍然会旋转。

解决方案是否仍然适用于纵向模式,或者我必须找到另一种方法来强制控制器仅处于纵向模式

查看您的 info.plist 文件。这里有一个名为 "Supported Interface Orientation" 的密钥,其中一个组用于 iPhone 版本,另一个用于 iPad.

这里可以删除值"Portrait (bottom home button)",替换为"Landscape (left home button)"或"Landscape (right home button)"。

希望对您有所帮助!

这样,你可以做到:

  1. 进入目标设置
  2. 转到开发信息
  3. 将设备方向更改为横向并取消选中所有其他选项。

希望对您有所帮助

我认为问题在于您在 info.plist 中定义了允许的方向,这显然会覆盖您在整个项目中其他任何地方所做的任何事情。

为了更正这个问题,我从 info.plist 中删除了条目并在项目设置中定义了它们。现在一切正常。

希望对您有所帮助。