问题描述
我还应该做什么?
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) toInterfaceOrientation { return (toInterfaceOrientation == UIInterfaceOrientationPortrait); } - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } -(BOOL)shouldAutoRotate { return NO; }
我的 viewController 还在旋转.
<小时>它嵌入在导航堆栈中.如果我将 UINavigationController 子类化,并在那里实现相同的仅纵向模板,并将我的 viewController 嵌入到经过调整的导航控制器中,那么它就可以工作,但我无意在 UINavigationController 出现的任何地方重写我的代码.
这里的最佳做法是什么?
推荐答案
原始答案:无需子类化 - 只需像我在此处的解决方案中描述的那样做一个类别:iOS6 中的顶部主页按钮纵向模拟器不工作
基本上,对于 iPhone,UINavigationController 允许旋转除"顶部主页按钮肖像"之外的所有内容,对于 iPad,它允许所有内容.
所以你要么做一个类别,将决定转发给当前活动的视图控制器,要么像静态的东西
UINavigationController-Rotation.h:
@interface UINavigationController (Rotation) @end
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 所指出的,如果有第二个类别做同样的事情,这可能会导致未定义的行为.在这种情况下,子类化可能是更好的解决方案.
在您知道没有其他类别可以这样做的情况下,我仍然认为这是一种可行、省力、本地且务实的解决方案.我对此并不虔诚.自己决定.