防止一个视图控制器自动旋转? [英] Prevent autorotate for one view controller?
问题描述
我的应用可以自动旋转,但我需要其中一个视图仅以纵向模式显示,但不知道如何实现.
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:
// ViewController.m
-(BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
有人可以指出我做错了什么吗?谢谢.
Can someone kindly point out what I'm doing wrong? Thanks.
-编辑-
适用于 iOS 6.1
It's for iOS 6.1
推荐答案
当涉及到 UINavigationController
时,在 UINavigationController
上创建一个类别并覆盖 supportedInterfaceOrientations代码>.
When a UINavigationController
is involved, create a category on the UINavigationController
and override supportedInterfaceOrientations
.
#import "UINavigationController+Orientation.h"
@implementation UINavigationController (Orientation)
- (NSUInteger)supportedInterfaceOrientations {
return [self.topViewController supportedInterfaceOrientations];
}
- (BOOL)shouldAutorotate {
return YES;
}
@end
现在,iOS 容器(例如 UINavigationController)不会咨询他们的孩子来确定他们是否应该自动旋转.
Now, iOS containers (such as UINavigationController) do not consult their children to determine whether they should autorotate.
如何创建类别
1.添加一个新文件(Cocoa touch下的Objective c-category)
2. Category
: UINavigationController 的方向
3.在UINavigationController+Orientation.m
How to create a category
1. Add a new file (Objective c- category under cocoa touch)
2. Category
: Orientation on UINavigationController
3. Add the above code to UINavigationController+Orientation.m
这篇关于防止一个视图控制器自动旋转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!