2012-06-18 114 views
2

我正在製作一個應用程序,只是爲了好玩,它似乎工作正常,除了事實上,當我希望它只在一個屏幕上的風景,它將保持肖像。我怎樣才能使它在加載時自動旋轉到橫向? 我已經試過這個:iOS:如何在另一個視圖向其轉動時旋轉視圖控件?

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight) || (interfaceOrientation == UIInterfaceOrientationLandscapeLeft); 
} 

但是,這似乎並沒有工作。只有當我旋轉設備時,它纔會旋轉視圖,然後它會保持橫向,但我希望它能夠在加載時儘快完成,而不必讓用戶旋轉設備以將其從肖像中移出。有什麼我失蹤的?感謝您的幫助!如果你想從應用程序獲得更多的代碼,我將非常樂意與你分享。

回答

3

在viewDidLoad方法和shouldautorotate方法補充一點:

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO]; 
[[self view] setBounds:CGRectMake(0, 0, 480, 320)]; 
[[self view] setCenter:CGPointMake(160, 240)]; 
[[self view] setTransform:CGAffineTransformMakeRotation(M_PI/2)]; 
0

試試這個代碼

@interface UIViewController(ESUtils) 
    -(void)changeToPortrait; 
@end 

@implementation UIViewController(ESUtils) 
-(void)changeToPortrait 
{ 
    //for will call a shouldAutorotateToInterfaceOrientation func 
    UIViewController *view = [[UIViewController alloc] init]; 
    [self presentModalViewController:view animated:NO]; 
    [self dismissModalViewControllerAnimated:NO]; 
    [view release]; 
} 

@end 
0

你想要做的兩件事情,

首先在界面生成器,去你想要的景觀景觀,並確保方向設置爲橫向(如果其推斷它將採取父母的方向)。您可以在屬性檢查器的第4個選項卡上的模擬度量標準下找到它。

現在第二,這種觀點必須有你貼

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight) || (interfaceOrientation == UIInterfaceOrientationLandscapeLeft); 
} 
代碼