iOS - UISupportedInterfaceOrientations 和 shouldAutorotateToInterfaceOrientation

标签 ios xcode cocoa-touch orientation

我正在寻找有关如何仅允许 iOS 应用程序使用某些方向的说明。我知道 UISupportedInterfaceOrientationsshouldAutorotateToInterfaceOrientation 但我对它们的用途以及它们如何组合在一起有点困惑。

我尝试使用 UISupportedInterfaceOrientations 来仅允许横向方向,这似乎没有任何影响,直到我研究它并了解到它会影响初始方向。经过测试,我的应用程序似乎只能在横向模式下打开,但如果屏幕是纵向模式,则会快速旋转。

我知道您可以使用shouldAutorotateToInterfaceOrientation来限制允许的方向,例如:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || 
           (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

但是,在网上阅读时,我读到 shouldAutorotateToInterfaceOrientation 自 iOS6 起已被弃用。

基本上我的问题是:

  1. 限制屏幕方向的正确方法是什么? 多个版本的 iOS?
  2. 是唯一使用UISupportedInterfaceOrientations来限制 初始定位?

编辑:

为了扩展已接受的答案,shouldAutorotate 适用于 iOS6。作为快速修复,如果您已经在 shouldAutorotateToInterfaceOrientation 中实现了逻辑并且/或想要支持早期版本的 iOS,则可以执行以下操作:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || 
           (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

- (BOOL)shouldAutorotate {
    return [self shouldAutorotateToInterfaceOrientation:self.interfaceOrientation];
}

最佳答案

您需要使用的旋转方法而不是 shouldAutorotateToInterfaceOrientation 只是 shouldAutorotate

根据 ViewController 的 AppleDoc 处理旋转:

In iOS 6, your app supports the interface orientations defined in your app’s Info.plist file. A view controller can override the supportedInterfaceOrientations method to limit the list of supported orientations. Generally, the system calls this method only on the root view controller of the window or a view controller presented to fill the entire screen; child view controllers use the portion of the window provided for them by their parent view controller and no longer participate in directly in decisions about what rotations are supported. The intersection of the app’s orientation mask and the view controller’s orientation mask is used to determine which orientations a view controller can be rotated into.

You can override the preferredInterfaceOrientationForPresentation for a view controller that is intended to be presented full screen in a specific orientation.

方法shouldAutorotateToInterfaceOrientation已弃用,一些处理设备旋转响应的方法也已弃用。

对于多版本iOS的支持方法,Apple还有这样的说法:

For compatibility, view controllers that still implement the shouldAutorotateToInterfaceOrientation: method do not get the new autorotation behaviors. (In other words, they do not fall back to using the app, app delegate, or Info.plist file to determine the supported orientations.) Instead, the shouldAutorotateToInterfaceOrientation: method is used to synthesize the information that would be returned by the supportedInterfaceOrientations method.

取自 release notes

关于iOS - UISupportedInterfaceOrientations 和 shouldAutorotateToInterfaceOrientation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13646134/

相关文章:

ios - 按需加载 UIScrollView

ios - Angular 2 数字键盘启动

ios - 在 Xcode 4.2 中使用架构特定设置

跨不同构建目录的 Xcode 依赖项?

objective-c - Storyboard/ Nib 与否

objective-c - Objective-C 字符串文字的预处理器宏值

objective-c - 如何从 NSArray 获取特定长度的字符串?

ios - 无法在 swift 中将类型 'UILabel!' 的值转换为预期的参数类型 'String'

iphone - 没有 GameKit 的 iOS 蓝牙

ios - dismissModalViewControllerAnimated 需要先完成动画,然后再从 super View 中删除?