iOS装置:特殊风景模式

问题描述:

我的应用程序只有人像模式,如下图: Device OrientationiOS装置:特殊风景模式

但它需要支持横向模式自定义的UIViewController。我有如下重写功能:

- (BOOL)shouldAutorotate 
    { 
     return NO; 
    } 

    - (UIInterfaceOrientationMask)supportedInterfaceOrientations 
    { 
     return UIInterfaceOrientationMaskLandscape; 
    } 

    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
    { 
     return UIInterfaceOrientationLandscapeRight; 
    } 

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
    { 
     return UIInterfaceOrientationIsLandscape(interfaceOrientation); 
    } 

它的工作在大多数情况下,但不知何故,它会表现得像如下形象: problem picture

似乎有设备的方向和MyViewController取向之间的冲突。

PS:我的设备是iPhone 6s Plus,系统是iOS 10.0,这个问题也存在于iPhone 6s中。

+0

我可以建议你做到这一点的替代方案吗? – KKRocks

+0

是的,PLZ,我想知道,thx – zxbeef

+0

检查我的答案。它在我的项目中运行良好。 – KKRocks

如果你想知道的ViewController特定的方向,那么请参考下面的回答
https://*.com/a/27010196/6325474

试试这个。

AppDelegate.h

@property() BOOL restrictRotation; 

AppDelegate.m

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
{ 
if(self.restrictRotation) 
    return UIInterfaceOrientationMaskPortrait; 
else 
    return UIInterfaceOrientationMaskAll; // you can change orientation for specific type . for ex landscape left 
} 

的ViewController

-(void) restrictRotation:(BOOL) restriction 
{ 
    AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate; 
    appDelegate.restrictRotation = restriction; 
} 

viewDidLoad中

[self restrictRotation:YES]; or NO 

你需要写这条线在每一个地方需要修改

更多的ViewController:https://*.com/a/25952571/3901620

+0

我的项目被其他开发者设计为静态库,所以修改AppDelegate可能不适合。 Thx都一样。 – zxbeef

+0

我的代码在大多数情况下运行良好,但MyViewController并未自转。我不知道为什么...... – zxbeef

可以在AppDelegate中。这使用下面的代码是唯一可行的解​​决方案,将工作 斯威夫特代码

func application(_ application: UIApplication, 
supportedInterfaceOrientationsFor window: UIWindow?) -> 
UIInterfaceOrientationMask { 

     let vc = self.window?.currentViewController() 
     if vc?.isKind(of: YourDesiredVCToChangeOrientation.self) == true { 
      return UIInterfaceOrientationMask.landscape; 
     } else { 
      return UIInterfaceOrientationMask.all ; 
     } 

    }