gpt4 book ai didi

iOS - UISupportedInterfaceOrientations 和 shouldAutorotateToInterfaceOrientation

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:12:46 25 4
gpt4 key购买 nike

我正在寻找有关如何仅允许您的 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

处理旋转,根据 AppleDoc for ViewControllers:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com