我的应用程序支持所有类型的方向,但我希望某些 viewController 仅支持横向,很少有人锁定 Potrait 模式。 我该怎么做,我试过下面的代码但没有任何效果。
- (BOOL)shouldAutorotate {
return NO;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
// pre-iOS 6 support
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}
最佳答案
推送到同一个导航 Controller 的所有 View Controller 必须支持相同的方向。具体来说,我相信只有 Root View Controller shouldAutorotateToInterfaceOrientation
会被考虑在内。你可以看看this S.O. post一种解决方法;或在 this one寻求可能有所帮助的不同方法。
我在上面链接的第二篇文章建议对 shouldAutorotateToInterfaceOrientation
使用此定义,您可以根据自己的情况对其进行调整:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([self.selectedViewController isKindOfClass:[UINavigationController class]]) {
UIViewController *rootController = [((UINavigationController *)self.selectedViewController).viewControllers objectAtIndex:0];
return [rootController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
关于iphone - 锁定 ViewController 的方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14174449/