防止一个视图控制器ios7的自动旋转? [英] Prevent autorotate for one view controller ios7?

查看:114
本文介绍了防止一个视图控制器ios7的自动旋转?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序可以自动旋转,但我需要其中一个视图才能以纵向模式显示,并且不知道如何实现此目的。
我尝试了这个(除其他外)但有问题的观点仍在轮换:

My app can autorotate but I need one of the views to only show in portrait mode and don't know how to achieve this. I tried this (among other things) but the view in question still rotates:

-(BOOL)shouldAutorotate
{            
    return NO;
}

 - (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}


推荐答案

此解决方案解释了如何控制单个视图控制器上的方向,只要它们由导航控制器管理。

This solution explains how to control orientation on individual view controllers, provided they are managed by a navigation controller.

在Xcode 5中,创建一个类型为Objective-C category的新文件,将其设置为类别到旋转并选择UINavigationController作为类别开启。

In Xcode 5, create a new file of type "Objective-C category", set it's "Category" to "rotation" and choose "UINavigationController" as "Category on".

项目中将出现一个新的文件对,具有以下名称:
UINavigationController + rotation.h
UINavigationController + rotation.m

A new file couple will appear in the project, having the following names: UINavigationController+rotation.h UINavigationController+rotation.m

在.m文件中,编写以下代码:

In the .m file, write the following code:

- (BOOL) shouldAutorotate
{
    return [[self topViewController] shouldAutorotate];
}

- (NSUInteger) supportedInterfaceOrientations
{
    return [[self topViewController] supportedInterfaceOrientations];
}

这样,导航控制器会让当前顶视图控制器确定方向策略。

This way, the navigation controller will let the current top view controller determine the orientation policy.

然后,在导航控制器管理的每个特定视图控制器中,您可以覆盖两个与方向相关的方法。

Then, in each specific view controller that is managed by the navigation controller, you can override the two orientation-related methods.

例如,如果特定视图控制器仅以纵向显示:

For instance, if a specific view controller shall appear in portrait orientation only:

- (BOOL) shouldAutorotate
{            
    return NO;
}

 - (NSUInteger) supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

确保所需的方向是项目部署信息中设置的方向之一。
希望这个内容足够详细,可以提供帮助。

Make sure that the desired orientation is one of those set in the project deployment info. Hope this is sufficiently detailed and can be of help.

这篇关于防止一个视图控制器ios7的自动旋转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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