如何在uinavigationcontrollerrootviewcontroller上禁用纵向



我有一个类,HomeView,作为我iPad应用程序上UINavigationController的rootViewController。只有横向方向在信息中设置。plist文件和HomeView不实现shouldRotateToInterfaceOrientation:,然而,当模拟器旋转时,视图在两个方向上旋转。

如何在我的UIViewController中只使用横向方向?

如果你不实现shouldAutorotateToInterfaceOrientation:运行时将调用Super类上的方法,UIViewController

相反,你应该适当地响应你想要旋转到的方向的方法:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    BOOL shouldAutorotate = NO;
    if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft
        || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        shouldAutorotate = YES;
    }
    return shouldAutorotate;
}

查看UIInterfaceOrientation参考页面和UIViewController的shouldAutorotateToInterfaceOrientation:参考页面了解更多信息

你不应该删除shouldAutorotateToInterfaceOrientation方法;那不会阻止接口旋转,它只是不会执行你在那个方法中可能有的任何代码。相反,您应该尝试这样做(假设您不希望界面旋转):

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return NO;
}

最新更新