4

如果将视图添加到窗口,即使设备处于横向,方向也会设置为纵向。如果在应用程序委托中添加视图,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];

有没有办法使视图具有正确的方向?

4

1 回答 1

1

确保视图控制器中的 shouldAutorotateToInterfaceOrientation 具有正确的逻辑

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

如果你在 InterfaceBuilder 中链接东西,还要确保视图和视图控制器都配置为你喜欢的初始方向。(右上角有个小箭头用来旋转视图和视图控制器)

如果您仍然有问题,您使用的是 UINavigationController 还是类似的?UINavigationController 需要子类化,如果你想支持除 portait 以外的东西,应该实现 shouldAutorotateToInterfaceOrientation。

于 2011-01-26T20:07:49.887 回答