我有一个带故事板的iOS应用程序。我希望我的上一个viewcontroller始终处于纵向模式。我一直在阅读,我发现自
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
已弃用我应该使用其他方法,例如
-(BOOL)shouldAutorotate
-(NSInteger)supportedInterfaceOrientations
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
但是我尝试了很多这种方法的组合,但是我无法做到。那么有人可以告诉我正确的方法吗?
答案 0 :(得分:5)
由于您的UIViewController嵌入在UINavigationController中,因此除非您自己转发呼叫,否则永远不会被调用。 (在我看来,UINavigationController有点瑕疵)
这样的子类UINavigationController:
@interface RotationAwareNavigationController : UINavigationController
@end
@implementation RotationAwareNavigationController
-(NSUInteger)supportedInterfaceOrientations {
UIViewController *top = self.topViewController;
return top.supportedInterfaceOrientations;
}
-(BOOL)shouldAutorotate {
UIViewController *top = self.topViewController;
return [top shouldAutorotate];
}
@end
答案 1 :(得分:1)
如果在其他UIViewControllers(即UINavigationController或UITabBarController)中有UIViewControllers,则需要将这些消息代理到您正在实现此行为的子对象。
您是否在实施中设置了断点以确保正在查询视图控制器?
答案 2 :(得分:1)
在AppDelegate中:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
NSUInteger orientations = UIInterfaceOrientationMaskAllButUpsideDown;
if(self.window.rootViewController) {
UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
orientations = [presentedViewController supportedInterfaceOrientations];
}
return orientations;
}
在ViewController中:
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}