2016-12-06 23 views
0

我的應用程序只有人像模式,如下圖: 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

回答

0

試試這個。

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://stackoverflow.com/a/25952571/3901620

+0

我的項目被其他開發者設計爲靜態庫,所以修改AppDelegate可能不適合。 Thx都一樣。 – zxbeef

+0

我的代碼在大多數情況下運行良好,但MyViewController並未自轉。我不知道爲什麼...... – zxbeef

0

可以在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 ; 
     } 

    }