2016-01-15 42 views
0

在iOS8上,我用如何在iPad Air2 iOS9.2的某些ViewController上強制屏幕方向?

- (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; 
} 

燦誰來幫幫我?謝謝!

回答

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; 

} 
+0

謝謝,但它不工作... –

+0

你選擇從項目設置 – ali

+0

我是否需要將其刪除所有其它的方向? –

1

你肯定是在info.plist中它覆蓋任何你做其他任何地方整個項目已定義的允許方向。

要解決該問題,您可以從info.plist中刪除條目並在項目設置中定義它們。

enter image description here

+0

我刪除了它們,但在項目設置中重新定義它們之後它又出現了。 –