我正在尝试为 UIViewController
设置方向并禁止该 UIViewController
上的所有方向更改。重要的是要注意该应用程序允许所有方向,但在这个单一 View Controller 上我只想允许纵向。
我正在使用 iOS 8,该应用程序适用于 iOS 6 及更高版本。所描述的行为发生在 iOS 7 和 8 中。我无法在 iOS 6 中测试它,所以我不确定它是否发生在 iOS 6 中。
我实现了以下方法:
- (BOOL)shouldAutorotate
{
return NO; // I've also tried returning YES - no success
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait; // I've also tried UIInterfaceOrientationPortrait - no success
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationMaskPortrait; // Never called
}
我已经能够为模态呈现的 View Controller 设置方向,但我没有为该项目以模态呈现 View Controller 。这甚至可以做到吗?
如何在不影响其他 UIViewController 的方向的情况下指定一个 UIViewController 的方向?这甚至可以做到吗?
最佳答案
假设您的 View Controller 嵌入在导航 Controller 中,您需要使用 UINavigationController
的自定义子类并添加:
- (BOOL)shouldAutorotate;
{
BOOL shouldAutorotate;
if ([self.topViewController respondsToSelector:@selector(shouldAutorotate)])
{
shouldAutorotate = [self.topViewController shouldAutorotate];
}
else {
shouldAutorotate = [super shouldAutorotate];
}
return shouldAutorotate;
}
- (NSUInteger)supportedInterfaceOrientations
{
NSUInteger supportedOrientations;
if ([[self topViewController] respondsToSelector:@selector(supportedInterfaceOrientations)]) {
supportedOrientations = [[self topViewController] supportedInterfaceOrientations];
}
else {
supportedOrientations = [super supportedInterfaceOrientations];
}
return supportedOrientations;
}
在你的导航 Controller 中。你的 View Controller 中的方法很好,所以让它们保持原样。这适用于 iOS 7-8(尚未在 iOS6 中测试)
关于ios - 在 UIViewController 上设置方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26386574/