2011-08-31 66 views
4

有沒有簡單的方法來手動設置界面方向?我需要將界面設置爲縱向,即使裝載期間設備方向可能處於橫向。有點想遠離CGAffineTransforms。手動設置界面方向

回答

16

一種方法,我知道這對我的作品(並且是一個黑客位,並可以改變到你想要的方向之前顯示一個方向)是:

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 

    UIApplication* application = [UIApplication sharedApplication]; 

    if (application.statusBarOrientation != UIInterfaceOrientationPortrait) 
    { 
     UIViewController *c = [[UIViewController alloc]init]; 
     [self presentModalViewController:c animated:NO]; 
     [self dismissModalViewControllerAnimated:NO]; 
     [c release]; 
    } 
} 
+0

這實際上對我描述的情況非常有效。但由於某些原因,不適合嘗試從縱向模式強制橫向(將UIInterfaceOrientationPortrait更改爲UIInterfaceOrientationLandscapeLeft)。想法? –

+0

實際工作的骯髒的黑客。 – beefon

+0

謝謝!偉大的代碼行=) – CReaTuS

2

覆蓋它來控制方向,直到裝...

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
+0

方向屬性爲只讀 –

+0

是的,您是對的。我的錯。我更新了我的答案。您可以考慮重寫shouldAutorotateToInterfaceOrientation並根據需要維護方向。 – Saran

2

首先,設置你的應用程序和觀點,只支持人像,然後使用從我的重構庫中提取的這一類方法,es_ios_utils

@interface UIViewController(ESUtils) 
    // Forces without using a private api. 
    -(void)forcePortrait; 
@end 

@implementation UIViewController(ESUtils) 

-(void)forcePortrait 
{ 
    //force portrait orientation without private methods. 
    UIViewController *c = [[UIViewController alloc] init]; 
    [self presentModalViewController:c animated:NO]; 
    [self dismissModalViewControllerAnimated:NO]; 
    [c release]; 
} 

@end 

該視圖在幀完成前關閉,將不會顯示。

+0

我可能從Shane或他的消息來源得到了這個。他有點更完整,我的建議使用一個類別來重用。 –

+0

謝謝!偉大的代碼行=) – CReaTuS

+0

哇,你能解釋更多關於它是如何工作的? – OMGPOP