如何在iPad Air2 iOS9.2上的某些ViewController强制屏幕方向?

时间:2016-01-15 13:46:00

标签: ios objective-c ipad screen uiinterfaceorientation

在iOS8我使用

- (BOOL)shouldAutorotate
{
    return NO;
}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationPortrait;
}

但不适用于iOS9 iPad Air2, 另外,我找到了另一个解决方案,但也没有工作, 在自定义UINavigationController中覆盖下面的函数:

-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return [self.viewControllers.lastObject supportedInterfaceOrientations];
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.viewControllers.lastObject preferredInterfaceOrientationForPresentation];
}
-(BOOL)shouldAutorotate{
    return self.visibleViewController.shouldAutorotate;
}

有人能帮助我吗?谢谢!

2 个答案:

答案 0 :(得分:1)

你当然已经在info.plist中定义了允许的方向,它取代了你在整个项目中其他地方所做的任何事情。

要解决此问题,您可以从info.plist中删除条目,并在项目设置中对其进行定义。

enter image description here

答案 1 :(得分:0)

在viewDidAppear中尝试这个:

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

这个答案可能会闪烁。如果您不想要闪烁,请尝试此选项,它将起作用:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if ([self.window.rootViewController.presentedViewController isKindOfClass: [SecondViewController class]])
{
    SecondViewController *secondController = (SecondViewController *) self.window.rootViewController.presentedViewController;
            return UIInterfaceOrientationMaskLandscapeRight;
}
return UIInterfaceOrientationMaskPortrait;

}