2010-09-15 24 views
4

如果視圖添加到窗口,則即使設備處於橫向,方向也會設置爲縱向。 如果在應用程序委託中添加視圖,則應用程序:didFinishLaunchingWithOptions:方法,然後它可以正常工作。但如果視圖稍後添加,它不會。更改UIWindow子視圖時,未設置方向

作爲一個例子,我有一個例程來切換視圖。最簡單的形式是:

- (void)switchToNewViewController:(UIViewController *)newViewController { 
if ([[window subviews]count]!=0) { 
    [[[window subviews]objectAtIndex:0] removeFromSuperview]; 
} 
[window addSubview:newViewController.view]; 
} 

如果在didFinishLaunching中調用此方法,則方向是正確的。如果不是,則方向是縱向。

簡單的情況是內didFinishLaunching我有以下兩行

// The following line works 
[self switchToNewViewController:fullScreenViewController]; 

// The following line which delays the method call until later results 
// in incorrect orientation 
[self performSelector:@selector(switchToNewViewController:) withObject:fullScreenViewController afterDelay:0.1]; 

有沒有一種方法,使視圖有正確的方向?

+0

你是否設法解決這個問題? – pt2ph8 2010-11-19 16:12:26

回答

1

確保您的視圖控制器shouldAutorotateToInterfaceOrientation有正確的邏輯

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
{ 
    return YES; //this supports all orientations 
} 

如果你在InterfaceBuilder中聯的東西也確保兩個視圖和視圖控制器配置爲你喜歡的初始方向。 (在右上角有一個小箭頭來旋轉視圖和查看控制器)

如果你仍然有問題,你使用的是UINavigationController還是類似的?如果您想要支持除portait以外的其他內容,則需要將UINavigationController實現子類,並執行shouldAutorotateToInterfaceOrientation。

+0

這是部分問題。我記得,Apple開發支持提出了兩個問題。重要的是交換的所有視圖都支持相同的方向,並且一次只有一個控制器應該控制屏幕。還有一些iOS3.2的已知問題在以後的版本中得到糾正。 – David 2011-11-26 01:49:17