如何在 ios9 中禁用旋转?

How to disable rotation in ios9?

我想禁用我的应用程序的旋转,它在 iOS 9 中有效。我试过了

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];

但是没有人工作。谁知道?

这应该从 iOS 6 开始工作,但我只在 iOS 8 上测试过它。子类 UINavigationController 并覆盖以下方法:

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationLandscapeRight;
}

- (BOOL)shouldAutorotate {
    return NO;
}

或者询问可见视图控制器

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return self.visibleViewController.preferredInterfaceOrientationForPresentation;
}

- (BOOL)shouldAutorotate {
    return self.visibleViewController.shouldAutorotate;
}

并在那里实施方法。

<key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
    </array>

在 Plist 文件中添加上面的键,并根据您的要求添加方向。

我加了一个例子

工作方式如下: 把这个写在 ViewController.m

-(BOOL)shouldAutorotate { return 否; }

在 Xcode 7.2、iOS 9.2、iPhone 6s Plus 中尝试过。