我的应用程序中有10个视图控制器,我使用导航控制器来加载/卸载它们。
除了一个视图控制器以外,其余的所有视图控制器都是竖屏模式。假设第7个视图控制器是横屏模式,当它被加载时我需要它以横屏模式呈现。
请建议一种方法来强制在IOS 6中从纵向模式转换为横向模式(最好在IOS 5中也适用)。
这是我在IOS 6之前的操作方法:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
UIViewController *c = [[[UIViewController alloc]init] autorelease];
[self presentModalViewController:c animated:NO];
[self dismissModalViewControllerAnimated:NO];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
弹出和关闭模态视图控制器会迫使应用程序重新审视其方向,因此shouldAutorotateToInterfaceOrientation
方法会被调用。
我在iOS 6中尝试的内容:
- (BOOL)shouldAutorotate{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationLandscapeLeft;
}
加载时,控制器一直呈现为横屏状态。在旋转设备后,方向就会正常改变。但是我需要让控制器在加载时自动旋转到横屏状态,这样用户就必须旋转设备才能正确查看数据。另一个问题:在将设备旋转回纵向后,虽然我已经在
supportedInterfaceOrientations
中指定了UIInterfaceOrientationMaskLandscape
,但方向仍然保持为纵向。为什么会这样呢?此外,以上3种方法都没有被调用。 一些有用的数据:
- 在我的plist文件中,我指定了除倒置之外的3个方向。
- 该项目是在Xcode 4.3 IOS 5中启动的。所有类(包括xib文件)都是在Xcode 4.5 IOS 6之前创建的,现在我使用的是最新版本。
- 在plist文件中,状态栏设置为可见。
- 在要在横屏中显示的xib文件中,状态栏为“无”,方向设置为横屏。