ios7 - iOS8 iOS7 和 iOS6 阻止模态呈现的 View Controller 的旋转

标签 ios7 ios6 ios8 screen-rotation presentviewcontroller

我几乎没有使用 presentViewController:animated: 方法模态呈现的 View Controller 。 我不想让应用程序在 iPad 上显示它们时旋转,因为它们都使用 FXBlurView 模糊了背景。旋转起来很麻烦,而且有时旋转呈现的 View 会弄乱呈现的 View 。

我需要支持 iOS6、7 和 8,除了私有(private) API setOrientation: 方法之外找不到任何解决方案。

最佳答案

我找到的唯一解决方案是创建一个导航 Controller 子类并在该导航 Controller 中包含模态视图 Controller 。
这意味着您将必须呈现导航 Controller ,该 Controller 将具有您最初想要呈现的 Root View Controller 。

这是非旋转(纵向)导航 Controller 的示例实现:

@implementation NonRotatingNavigationController

- (BOOL)shouldAutorotate {
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}

@end

您也可以“询问”显示的 View Controller ,而不是决定支持哪个方向。
像这样:

@implementation NonRotatingNavigationController

- (BOOL)shouldAutorotate {
    return [[[self viewControllers] lastObject] shouldAutorotate];
}

- (NSUInteger)supportedInterfaceOrientations {
    return [[[self viewControllers] lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return [[[self viewControllers] lastObject] preferredInterfaceOrientationForPresentation];
}

@end

编辑(添加了替代解决方案)

另一种解决方案是添加一个包含 View Controller 的单独窗口。
这次您必须在 View Controller 本身内部实现旋转方法,并将其设置为新窗口的 Root View Controller 。

这是窗口创建代码(重要的是窗口将由某人拥有 - 例如原始 View Controller 中的 strong 属性 - 否则它将被释放,你将什么也看不到):

@interface ViewController ()
@property (nonatomic, strong) UIWindow *modalWindow;
@end

@implementation ViewController

- (IBAction)buttonTapped:(id)sender {

    NonRotatingViewController *vc = [[self storyboard] instantiateViewControllerWithIdentifier:@"NonRotatingViewController"];

    UIWindow *modalWindow = [[UIWindow alloc] initWithFrame:self.view.window.frame];
    modalWindow.windowLevel = UIWindowLevelAlert;
    modalWindow.backgroundColor = [UIColor clearColor];
    [modalWindow setRootViewController:vc];
    [modalWindow makeKeyAndVisible];

    self.modalWindow = modalWindow;

}

@end

关于ios7 - iOS8 iOS7 和 iOS6 阻止模态呈现的 View Controller 的旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26830554/

相关文章:

objective-c - 以编程方式设置 AutoLayout 大小类?

xcode - 将上下文管理器添加到单个 View 应用程序

ios - 将按钮列表与自动布局水平对齐

iOS - UIView 总是在前面,没有 BringSubviewToFront

iPhone - iPhone5 对现有应用程序的支持?

javascript - 如果其中有 setTimeout,则不会调用 iOS 6 js 事件函数

ios - 如何在 iOS 8 中将用户限制为只有一个默认键盘?

xcode - 我可以将带有 iOS 8 扩展的应用程序部署到运行 iOS 7 的设备吗?

ios - Xcode 5 和 Assets 目录 : How to reference the LaunchImage?

iphone 5 的 xcode 4.5 依赖性问题