如果将视图添加到窗口,即使设备处于横向,方向也会设置为纵向。如果在应用程序委托中添加视图,application:didFinishLaunchingWithOptions: 方法,那么它可以正常工作。但是,如果稍后添加视图,则不会。
例如,我有一个切换视图的例程。最简单的形式是:
- (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];
有没有办法使视图具有正确的方向?