UINavigationController 强制旋转

UINavigationController Force Rotate(UINavigationController 强制旋转)

本文介绍了UINavigationController 强制旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序主要是纵向的,但是有一个视图需要横向.

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 强制旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:UINavigationController 强制旋转

基础教程推荐