iphone - UINavigationController 强制旋转

标签 iphone objective-c ios ios5 uinavigationcontroller

我的应用程序主要是纵向的,但是有一个 View 需要横向。

我的 View 包含在 UINavigationController 中,这(显然)是导致此问题的原因。

所有的 UIViewControllers 除了一个有这个:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

需要 Landscape 的 UIViewController 有这个:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

现在,当用户到达横向 UIViewController 时,它会以纵向显示。然后用户可以旋转他们的手机,它会按照我的意愿横向显示(锁定横向)。然后用户继续前进到竖屏 UIViewController,同样的事情发生了:它从横屏开始,然后他们旋转他们的手机,它再次变成竖屏(并锁定为竖屏)。

似乎 UIViewController 之间允许方向锁定,但是自动旋转/以编程方式更改方向以某种方式被阻止。

如何强制手机更新到正确的方向?

有一个临时解决方案:我可以检测设备的方向并在不正确时显示一条消息要求他们旋转设备,但这不是最佳的。

最佳答案

我的一个应用程序也有同样的要求!!!

幸运的是我找到了解决方案!

为了保持主 viewcontroller 景观,无论从哪个方向弹出/插入,我做了以下事情:(在 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 上测试。

附言在 iOS 8 上,之前的答案导致一些屏幕闪烁,而且 - 它不稳定(在某些情况下它不再对我有用。)所以,为了我的需要,我将代码更改为:(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.

希望对您有所帮助!

关于iphone - UINavigationController 强制旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9826920/

相关文章:

iphone - 添加 UIButtonTypeDetailDisclosure - 逻辑问题

ios - 如何创建一个 NSManagedObjectContext 作为另一个 NSManagedObjectContext 的子集?

iOS FBLoginView : the user is not allowed to see this application per the developer set configuration

c++ - iOS中Opencv人脸检测Cascade编译器报错

iphone - 如何在 UINavigationBar 中设置自定义字体?

iphone - 编辑 TableView 时自定义 TableCell 中没有缩进

ios - CoreData - 更新模型类而不是创建新的

ios - 我可以在 Interface Builder 中设计一个带有静态内容的 UITableViewController 吗?

ios - (Swift) 将视频输出保存到文件

ios - 我可以在 iTunes Connect 和 Xcode 中跳过 iPhone 应用程序的版本号吗?