2009-01-10 79 views
9

好吧,我有我的正常應用程序,這是在肖像模式。我可以強迫我的應用程序去橫向模式視圖(使用navigationcontroller和ViewController)是這樣的:iPhone SDK:方向(橫向和縱向視圖)

- (void)viewWillAppear:(BOOL)animated { 
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight]; 
} 

但是當我回到主菜單(tableview中),它會直接回到畫像。我試試這個代碼:

- (void)viewWillAppear:(BOOL)animated { 
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait]; 
} 

但是,這並不工作..

任何想法?

回答

9

查看UIViewController類中的函數shouldAutorotateToInterfaceOrientation:。如果您的UIView支持方向,則此函數返回YES。如果您僅將YES返回到橫向,則iPhone會自動放入該方向。

下面的代碼應該這樣做。把它放在UIViewController中,該UIViewController控制你想放在橫向模式下的視圖。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationLandscape); 
} 
0

您需要編輯Info.plist文件以添加具有適當值(UIInterfaceOrientationLandscapeRight或UIInterfaceOrientationLandscapeLeft)的UIInterfaceOrientation鍵。

+0

是,僅僅回到一個畫像表主屏幕?因爲我想要一切肖像,只有一個景觀。我可以看到它的風景,但是當我回去時,我必須旋轉手機才能將它恢復成肖像。我不希望用戶有其他的風景。 – Domness 2009-01-10 16:26:52

7

爲了得到這個工作,我已將此添加到RootViewController的:

- (void)viewWillAppear:(BOOL)animated { 
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait]; 

} 

這似乎是現在的工作。

+0

這算作使用私人API嗎?方向被記錄爲只讀屬性。 – lawrence 2009-05-16 19:29:41

+0

這一切都很好,通過蘋果:) – Domness 2009-05-17 01:18:22

3

下面是我在做什麼這樣做:

首先,把這個定義在文件的頂部,就在你#imports:

#define degreesToRadian(x) (M_PI * (x)/180.0) 

然後,在viewWillAppear中:方法

[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];  
if (self.interfaceOrientation == UIInterfaceOrientationPortrait) { 
    self.view.transform = CGAffineTransformIdentity; 
    self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90)); 
    self.view.bounds = CGRectMake(0.0, 0.0, 480, 320); 
} 

,如果你想要的是動畫,那麼你可以用整個事情在動畫塊,像這樣:

[UIView beginAnimations:@"View Flip" context:nil]; 
[UIView setAnimationDuration:1.25]; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 

[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];  
if (self.interfaceOrientation == UIInterfaceOrientationPortrait) { 
    self.view.transform = CGAffineTransformIdentity; 
    self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90)); 
    self.view.bounds = CGRectMake(0.0, 0.0, 480, 320); 
} 
[UIView commitAnimations]; 

然後,在您的肖像模式控制器中,您可以執行相反的操作 - 檢查當前是否在橫向,如果是,則將其旋轉回肖像。

0

您不能使用此私有API。

有一個解決方案來做到這一點:它使用視圖控制器並將其視圖添加到窗口。那麼在那個控制器中,你會在shouldAutorotate ...方法中強制橫向。它工作正常,但要確保你的項目需要使用它,因爲強迫用戶轉動他的iPhone並不是很聰明。順便說一句,如果你需要的話,這裏是一個示例代碼。

http://www.geckogeek.fr/iphone-forcer-le-mode-landscape-ou-portrait-en-cours-dexecution.html