iOS 6.0限制导航控制器内的自动旋转? [英] iOS 6.0 restrict auto rotation within a navigation controller?
问题描述
我还应该做什么?
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) toInterfaceOrientation
{
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
-(BOOL)shouldAutoRotate
{
return NO;
}
我的viewController仍在旋转。
My viewController still rotates.
它嵌入在导航堆栈中。
如果我是UINavigationController的子类,并在那里实现相同的只有纵向模板,并且我将viewController嵌入到调整后的navigationController中,而不是它的工作原理,但我无意在UINavigationController出现的任何地方重写我的代码。
It is embedded in a navigation stack. If I subclass UINavigationController, and implement the same portrait-only templates there, and I embed my viewController in that tweaked navigationController, than it works, but I have no intention to rewrite my code everywhere a UINavigationController appears.
这里的最佳做法是什么?
What is the best practice here?
推荐答案
原始答案:无需子类 - 只需像我在我的解决方案中描述的那样进行类别:
iOS6模拟器中的顶级主页按钮纵向无法工作
ORIGINAL ANSWER: No need to subclass - just do a category like I described in my solution here: Top-home button portrait orientation in iOS6 simulator not working
基本上,对于iPhone,UINavigationController允许旋转一切除了顶级主页按钮肖像,对于iPad,它允许一切。
Basically, for iPhone the UINavigationController allows rotation for everything except "top home button portrait", for iPad it allows everything.
因此,您要么将类别转发给当前活动的视图控制器或静态像
So either you do a category forwarding the decision to the currently active view controller or something static like
U. INavigationController-Rotation.h:
UINavigationController-Rotation.h:
@interface UINavigationController (Rotation)
@end
UINavigationController-Rotation.m:
UINavigationController-Rotation.m:
#import "UINavigationController-Rotation.h"
@implementation UINavigationController (Rotation)
#pragma From UINavigationController
- (BOOL)shouldAutorotate {
return NO;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
#pragma -
@end
更新:正如Javier Soto指出的那样,如果第二个类别做同样的事情,这可能会导致未定义的行为。在这种情况下,子类化可能是一个更好的解决方案。
UPDATE: As Javier Soto pointed out, this might lead to undefined behavior if there is a second category doing the same. In that case, subclassing might be a better solution.
在你知道没有其他类别做同样事情的情况下,我仍然认为这是一个有效的工作,本地务实的解决方案。我对此并不信任。决定你自己。
In a situation where you know there is no other category doing the same I still consider this a working, low effort, local and pragmatic solution. I am not religious about that. Decide yourself.
这篇关于iOS 6.0限制导航控制器内的自动旋转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!