强制改变方向

人气:700 发布:2022-10-16 标签: ios objective-c cocoa-touch uikit

问题描述

我有一个基于选项卡的应用程序,其中一个选项卡在纵向和横向模式下均可用,所有其他选项卡仅在纵向模式下可用.

I have a tab based application in which one tab is available in both portrait and landscape mode, all the others are only available in portrait.

我正在检查是否允许在 shouldAutorotateToInterfaceOrientation: 中旋转的按钮,但是当我处于横向模式时,当我选择不同的选项卡时,我需要加载该视图控制器,但还要强制我的应用程序进入正常的纵向布局模式.

I am checking against the button selected to allow rotation in shouldAutorotateToInterfaceOrientation: or not, but when I am in landscape mode, when I select a different tab I need to load that view controller but also force my application into the normal portrait layout mode.

似乎没有一个明确且首选的方法是这样做,我尝试设置状态栏方向,但状态栏是唯一可以移动的视觉元素.

There doesn't seem to be a clear and preferred was to do this, I tried setting the status bar orientation but the status bar was the only visual element to move.

任何提示和示例都会很棒,谢谢.

Any tips and examples would be great, thanks.

推荐答案

我能够做到这一点,但我现在警告你,这是一个 hack.

I was able to do this, but I'm warning you now, it's a hack.

首先,我为 UITabBarController 创建了一个类别,名为UITabBarController+SelectiveRotation:

First, I created a category for UITabBarController called UITabBarController+SelectiveRotation:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return [self.selectedViewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
}

这允许 TabBar 在所选选项卡允许方向时自由旋转.确保在创建 UITabBarController 的任何位置(可能在您的应用程序委托中)导入此文件.

This allows the TabBar to rotate freely whenever the selected tab allows an orientation. Make sure you import this file wherever you're creating your UITabBarController (probably in your application delegate).

然后,我让我的 AppDelegate 让自己成为标签栏的代表,然后我实现了这个方法:

Then, I had my AppDelegate make itself the delegate of the tab bar, and I implemented this method:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    // this ensures that the view that will be shown is presented in a supportred rotation
    UIViewController *c = [[UIViewController alloc]init];
    [viewController presentModalViewController:c animated:NO];
    [viewController dismissModalViewControllerAnimated:NO];
    [c release];
    if ([UIViewController respondsToSelector:@selector(attemptRotationToDeviceOrientation)]) {
        // this ensures that the view will be presented in the orientation of the device
        // This method is only supported on iOS 5.0.  iOS 4.3 users may get a little dizzy.
        [UIViewController attemptRotationToDeviceOrientation];
    }
}

当标签改变时,该第二位代码强制标签栏旋转到可接受的方向.例如,如果您转到可以旋转的选项卡,然后转到横向,然后转到仅支持纵向的选项卡,它将强制将方向更改为纵向.在 iOS 4.3 及更早版本上,返回旋转的选项卡会以您离开时的方向显示它.我想不出办法.

This second bit of code forces the tab bar to rotate to an acceptable orientation when the tab changes. For instance, if you go to the tab that can rotate, and go to landscape, then go to a tab that only supports portrait, it'll force the orientation change to portrait. On iOS 4.3 and earlier, going back to the rotated tab will present it in the orientation you left it in. I couldn't figure out a way around that.

我做这一切是因为这是客户的要求,但我不认为这实际上是一个非常有用的设计.我没有发现太多视觉上的调整会让人迷失方向,但是通过这种方法强制设备旋转是非常不和谐的,因为它是瞬时的.您可能想尝试一下,看看您的感受.

I did all this because it was a client requirement, but I don't think this is actually a very usable design. I don't find too many visual tweaks to be disorienting, but forcing the device to rotate by this method is very jarring because it's instantaneous. You might want to try it out and see how you feel about it.

143