强制方向更改 [英] Forcing orientation change

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

问题描述

我有一个基于标签的应用程序,其中一个标签在纵向和横向模式都可用,所有其他标签只能在纵向模式下使用。



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



似乎没有一个明确的选择,我试图设置状态栏的方向,但状态栏是唯一的视觉元素移动。

$

解决方案

我可以做任何提示和示例



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

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

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



,我有我的AppDelegate使自己的标签栏的代理,我实现这个方法:

   - (void)tabBarController: (UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
//这确保将显示的视图呈现在支持旋转
UIViewController * c = [[UIViewController alloc] init];
[viewController presentModalViewController:c animated:NO];
[viewController dismissModalViewControllerAnimated:NO];
[c release];
if([UIViewController respondingToSelector:@selector(attemptRotationToDeviceOrientation)]){
//这确保视图将以设备的方向显示
//此方法仅支持iOS 5.0。 iOS 4.3用户可能会有点头晕。
[UIViewController attemptRotationToDeviceOrientation];
}
}

这第二位代码强制标签栏旋转到标签改变时的可接受的取向。例如,如果您转到可以旋转的标签,并转到横向,然后转到只支持纵向的标签,它将强制方向更改为纵向。在iOS 4.3及更早版本中,回到已旋转的标签页将会按照您留下的方向显示它。我无法找到解决方法。



我所有这一切,因为它是一个客户的要求,但我不认为这实际上是一个非常有用的设计。我没有发现太多的视觉调整是迷失方向,但强迫设备旋转这种方法是非常令人不安,因为它是即时的。你可以尝试一下,看看你的感受。


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.

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.

解决方案

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

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

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

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).

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];
    }
}

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.

这篇关于强制方向更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆