2014-02-14 67 views
0

我有一個視圖以全屏顯示。我希望呈現的全屏視圖僅限於肖像。肖像取向

任何人都可以幫助我將限制只有一個視圖作爲肖像?它不應該轉向景觀。

+0

它是一個單獨的視圖控制器或只是一個UIView? – David

+0

單獨的viewController。總而言之,我有兩個viewControllers.For第二個我想限制爲肖像。 – user3202087

+0

SEE:http:// stackoverflow。com/questions/18595561/alternative-ios-layouts-for-portrait-and-landscape-using-just-one-xib-file –

回答

2

您使用的代碼取決於你的目標iOS版:

的iOS 6+

有一個叫supportedInterfaceOrientations方法,做你在找什麼:

- (NSUInteger)supportedInterfaceOrientations 
{ 
    //If you want to support landscape 
    return UIInterfaceOrientationMaskAll; 

    //If you don't 
    return UIInterfaceOrientationMaskPortrait; 
} 

- (BOOL)shouldAutorotate { 
    return YES; 
} 

根據放每個視圖控制器中的return語句。


的iOS 5和前:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { 
    return !(UIInterfaceOrientationIsLandscape(toInterfaceOrientation)); 
} 
+0

是的,謝謝它在我的模擬器上工作,我會建立它iPad和檢查,並給你的反饋 – user3202087

+0

嘿它不是在我的主要應用程序,其中有很多viewControllers呈現在一個我想限制到肖像那裏我添加上面的代碼,但它不工作。景觀也。你有其他方式嗎? – user3202087

+0

你寫的這個iOS版本是什麼? – David

1

如果您使用的是模型風險投資之間呈現,@大衛的答案是正確的。 但是,如果您使用UINavigationController push/pop,事情會變得更加複雜。

- (NSUInteger)supportedInterfaceOrientations- (BOOL)shouldAutorotate僅在設備旋轉或根控制器更改後纔會調用。推/流行將不會使它。


在iOS5中和之前,有一個API:

[[UIDevice currentDevice] setOrientation: UIInterfaceOrientationPortrait]; 

但它已被否決。蘋果讓它變得私密。

你仍然可以使用objc運行時API來調用它的iOS 6+,如:

objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), @(UIInterfaceOrientationPortrait)); 

[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationPortrait) 
          forKey:@"orientation"]; 

注意:它是私有的API,這可能是由被拒絕App Store。


另一個棘手的方式是,只提出一個空的VC然後關閉它在viewDidAppear,是這樣的:

[self presentViewController:[UIViewController new] 
       animated:NO 
       completion:^{ 
        [self dismissViewControllerAnimated:NO completion:nil]; 
       }]; 

這將使- (NSUInteger)supportedInterfaceOrientations- (BOOL)shouldAutorotate被調用。


如果您對上述不滿意。儘量讓自己的導航控制器,或 檢查出這個問題:Why can't I force landscape orientation when use UINavigationController?

+0

感謝它運作良好。但有一個問題,當這個視圖打開它變成肖像,但是當我將它旋轉到左邊4次時,它是第四次進入景觀。 :( – user3202087

+0

@ user3202087我無法弄清楚爲什麼現在用你的描述,也許你可以問一個新的問題併發布代碼 – lancy

+0

其實,我使用的是navigationController push/pop,這就是爲什麼大衛的代碼不工作,你的代碼有一個問題(上面提到的一個),但我改變了推/流行呈現viewController。總之Thanx的幫助你ppl給。:) – user3202087