我的应用程序主要是纵向的,但是有一个视图需要横向.
My application is primarily portrait, however there is one view that REQUIRES a landscape orientation.
我的视图包含在 UINavigationController 中,这(显然)是导致此问题的原因.
My views are contained within a UINavigationController, which (apparently) is the cause of this issue.
除了一个之外的所有 UIViewControllers 都有这个:
All UIViewControllers except one have this:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
需要 Landscape 的 UIViewController 有这个:
The UIViewController that requires Landscape has this:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
现在,当用户到达横向 UIViewController 时,它会以纵向显示.然后,用户可以旋转他们的手机,它会按照我的意愿横向显示(锁定为横向).然后用户继续使用纵向 UIViewController,同样的情况发生:它从横向开始,然后他们旋转手机,它再次变成纵向(并锁定为纵向).
Now, what happens is when the user reaches the landscape UIViewController, it is shown in portrait. The user can then rotate their phone and it displays in landscape as I want it to (locking to landscape). The user then progresses onwards to a portrait UIViewController and the same happens: it start in landscape, then they rotate their phone and it becomes portrait again (and locks to portrait).
UIViewController 之间似乎允许方向锁定,但是自动旋转/以编程方式更改方向被某种方式阻止了.
It seems orientation locking is allowed between UIViewControllers, however auto-rotation / programmatically changing the orientation is somehow blocked.
如何强制手机更新到正确的方向?
有一个临时解决方案:我可以检测设备的方向并显示一条消息,要求他们在不正确时旋转设备,但这不是最佳选择.
我的一个应用程序也有同样的要求!!!
I had the same requirement for one of my applications!!!
幸运的是我找到了解决方案!
luckily I found a solution!
为了保持主视图控制器的横向,无论从哪个方向弹出/推送,我都做了以下事情:(在 viewWillAppear 中:)
//set statusbar to the desired rotation position
[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeLeft animated:NO];
//present/dismiss viewcontroller in order to activate rotating.
UIViewController *mVC = [[[UIViewController alloc] init] autorelease];
[self presentModalViewController:mVC animated:NO];
[self dismissModalViewControllerAnimated:NO];
P.S.在 sdk 3.2.5 ios 5.0.1 上测试.
P.S.Tested on sdk 3.2.5 ios 5.0.1.
附:在 iOS 8 上,先前的答案导致屏幕闪烁,而且 - 它不稳定(在某些情况下它不再适合我.)所以,为了我的需要,我将代码更改为:(ARC)
P.S. On iOS 8 previous answer results some screen flickering and also - it is not stable (In some cases It does not work for me anymore.) So, for my needs, I changed the code to: (ARC)
//set statusbar to the desired rotation position
[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeLeft animated:NO];
[self.navigationController presentViewController:[UIViewController new] animated:NO completion:^{
dispatch_after(0, dispatch_get_main_queue(), ^{
[self.navigationController dismissViewControllerAnimated:NO completion:nil];
});
}];
//I added this code in viewDidDissapear on viewController class, which will be popped back.
希望它会有所帮助!
这篇关于UINavigationController 强制旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
在没有应用程序源代码的情况下使用 Instruments 测Using Instruments to test an iOS app without having source code to the application(在没有应用程序源代码的情况下使用 Instruments 测试
KIF:如何自动运行/压力测试 iOS 应用程序以找出罕KIF: How to auto-run/stress test an iOS app to find the cause of a rare UI bug?(KIF:如何自动运行/压力测试 iOS 应用程序以找出罕见 UI 错
无法更改 Xcode 4.5 中的目标成员身份可见性Can#39;t change target membership visibility in Xcode 4.5(无法更改 Xcode 4.5 中的目标成员身份可见性)
UITableView:在混合单元格表视图静态和动态单元格UITableView: Handle cell selection in a mixed cell table view static and dynamic cells(UITableView:在混合单元格表视图静态和动态单元格中
如何在 iOS 中删除 Safari 中的地址栏?How to remove Address Bar in Safari in iOS?(如何在 iOS 中删除 Safari 中的地址栏?)
升级到 Xcode 4.5 后,iOS 5 SDK 消失了iOS 5 SDK is gone after upgrade to Xcode 4.5(升级到 Xcode 4.5 后,iOS 5 SDK 消失了)