8

我正在开发一个有几个笔尖的 iPhone 应用程序,并且应该只是横向的。

该应用程序通过其 Info.plist 文件设置为以横向模式启动。

我有两个视图控制器: FirstViewControllerSecondViewController.

对于其中的每一个,我都有一个 nib 文件,其中视图是横向的。两个视图控制器都MainView作为插座添加到我的 nib 中,并且它们的视图被延迟初始化。

当应用程序加载时,第一个视图按预期显示为横向。但是,当我切换到第二个视图时,设备(或模拟器)仍处于横向状态,但视图会旋转,就好像设备处于纵向模式一样,这会影响我的界面。

在这两个UIViewController类中,我都有以下代码:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return interfaceOrientation == UIInterfaceOrientationLandscapeRight;
}

并切换视图,在我的应用程序委托中我正在做:

[viewController.view removeFromSuperview];
[window addSubview:secondViewController.view];

whereviewControllersecondViewController是连接视图控制器的两个出口。

这是 IB 中第二个视图的外观: alt text http://img27.imageshack.us/img27/4898/picture1ni.png

这就是它在模拟器中的样子: alt text http://img402.imageshack.us/img402/4866/picture2wt.png

为什么第二个视图以横向显示但界面旋转?

我不想处理变换属性,因为这似乎有点过头了。

4

2 回答 2

3

我为这个问题加注了星标,希望有人能给你一个有见地的回答,我会学到一些东西.. 遗憾的是,我担心你可能需要使用转换来让它正常工作。这是我最近用来解决问题的代码:

- (void)forceLandscapeForView:(UIView *)theView {
  theView.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
  theView.bounds = CGRectMake(0, 0, 480, 320);
  theView.center = CGPointMake(160, 240);
  [theView setNeedsLayout];
  [theView setNeedsDisplay];
}

然后,当您添加新视图时,请检查当前方向,并在必要时强制旋转:

if (!UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)) {
  [self forceLandscapeForView:_activeViewController.view];
}

然后,您当然会希望 shouldAutorotateToInterfaceOrientation在每个视图控制器中做出适当的响应:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}

如果这不是全部必要的话,我很想听听其他解决方案。我注意到此设置还有一个警告:如果您在视图之间进行转换,并且在该转换期间旋转手机,则视图方向可能会翻转或“卡”在错误的横向方向上,例如当您在视图之间导航时,您需要将手机翻过来(横向与横向)。

于 2009-12-15T18:56:44.057 回答
0

这只是一个建议,但您可以尝试在第二个视图的 shouldAotorotate 方法中返回 NO。或者尝试在 IB 的纵向视图中制作它。您的视图似乎已正确加载(在横向模式下),但随后收到 shouldAutorotate 消息并已旋转 90 度。

于 2009-12-12T08:50:25.970 回答