2010-09-15 27 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