gpt4 book ai didi

iphone - 在 iOS7 中处理一个 View Controller 的自动旋转

转载 作者:技术小花猫 更新时间:2023-10-29 11:09:36 25 4
gpt4 key购买 nike

我已经阅读了很多关于 SO 的答案,但我似乎无法在 iOS7 上进行自动旋转。

我只需要一个 View Controller 来旋转,所以我不想在我的 Info.plist 中设置旋转设置。

据我了解 Apple 的文档,单个 View Controller 可以通过简单地覆盖两种方法来覆盖全局旋转设置(来自 Info.plist)。 Info.plist 设置为只允许纵向,我的 View Controller 实现了以下方法:

- (NSUInteger)supportedInterfaceOrientations
{
NSLog(@"%s", __PRETTY_FUNCTION__);
return UIInterfaceOrientationMaskAllButUpsideDown;
}

- (BOOL)shouldAutorotate
{
NSLog(@"%s", __PRETTY_FUNCTION__);
return true;
}

我在旋转时看到了那些 NSLog 语句,但没有旋转。

如果我使用适当的旋转设置配置 Info.plist,我的 View 将旋转,但如果我尝试依赖我的 View Controller 则不会。

不确定这是否重要,但我尝试旋转的 View 来自使用自动布局的 .xib。

此外,我的 ViewController 以模态方式呈现并包含在导航 Controller 中。我试过只单独呈现 View Controller ,但那行不通。我也试过 adding a category to UINavigationController从它的 topViewController 获取它的自转方向。

最佳答案

就我而言,我有一个新的 iOS7 应用程序,其中已经创建了大约 30 个 View Controller 。我只需要在一个模态视图 Controller 上自动旋转。我不想更新现有的 View Controller 。

我在 plist 中选择了我想要的方向:

select orientations

然后我在 UIViewController 上向我的应用委托(delegate)添加了一个类别:

@implementation UIViewController (rotate)
-(BOOL)shouldAutorotate {
return NO;
}
@end

然后在我想旋转的单一模态视图 Controller 中我添加了这个方法:

-(BOOL)shouldAutorotate {
return YES;
}

我还发现,如果我的 View Controller 不是模态 VC,我需要在 UINavigationController 上添加类别方法,对于 Root View Controller 之后的所有 VC,作为 View Controller 导航堆栈的一部分- 类似于:https://stackoverflow.com/a/20283331/396429

关于iphone - 在 iOS7 中处理一个 View Controller 的自动旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19502251/

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