【问题标题】:Disable autorotate on a single UIViewController not working in iPad Pro禁用在 iPad Pro 中不起作用的单个 UIViewController 上的自动旋转
【发布时间】:2016-03-22 05:14:52
【问题描述】:

我想在 iPhone 和 iPad 中锁定单个视图控制器。 下面的代码在 iPhone 4、5、6 iPad、iPad 2、iPad Retina 中完美运行。 但不适用于 iPad pro。

@implementation UINavigationController (Orientation)
-(NSUInteger)supportedInterfaceOrientations
{
        return [self.topViewController supportedInterfaceOrientations];
}

-(BOOL)shouldAutorotate
{
    return YES;
}
@end

上面的代码写在我不想旋转的视图控制器中。

【问题讨论】:

  • 什么是 iPad pro 上的 iOS 版本
  • 永远不要尝试使用类别覆盖方法。这是未定义的行为。
  • 我尝试了以下解决方案,但它不起作用@Chetan
  • 请查看此网址stackoverflow.com/questions/35794712/… 可能对您有所帮助

标签: ios objective-c orientation portrait


【解决方案1】:

在视图控制器中编写下面的代码,您希望在纵向模式下锁定哪个视图控制器

@implementation UINavigationController (Orientation)
-(NSUInteger)supportedInterfaceOrientations
{
        return [self.topViewController supportedInterfaceOrientations];
}

-(BOOL)shouldAutorotate
{
    return YES;
}
@end

#pragma mark Orientation
-(BOOL)shouldAutorotate
{
    [super shouldAutorotate];
    return NO;
}
-(NSUInteger) supportedInterfaceOrientations {
    [super supportedInterfaceOrientations];
    // Return a bitmask of supported orientations. If you need more,
    // use bitwise or (see the commented return).
    return UIInterfaceOrientationMaskPortrait;
    // return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}

- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation {
    [super preferredInterfaceOrientationForPresentation];
    // Return the orientation you'd prefer - this is what it launches to. The
    // user can still rotate. You don't have to implement this method, in which
    // case it launches in the current orientation
    return UIInterfaceOrientationPortrait;
}

现在在您的 plist 文件中执行以下更改

【讨论】:

  • 谢谢!我以为我已经尝试了一切。在项目信息上设置正确的复选框,选中全屏,覆盖正确的方法。他们甚至被召唤。我通过进入 info.plist 并删除 iPad 的方向解决了这个问题。愚蠢的是,它们没有显示在通用项目的项目信息中。
【解决方案2】:

把它写在你不想旋转的视图控制器中

这将防止任何旋转。

你不想旋转的视图控制器类应该有这个。

- (BOOL)shouldAutorotate
{
    return NO;
}

The containing navigation controller class should have this.

- (BOOL)shouldAutorotate
{
    return [self.topViewController shouldAutoRotate];
}

这只会旋转到纵向

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

【讨论】:

  • 不要在视图控制器中声明这两种方法。一种是在视图控制器中声明,另一种是在导航控制器类中声明。再次阅读答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-06
  • 2012-09-15
  • 1970-01-01
  • 2016-01-23
  • 2011-05-06
  • 1970-01-01
相关资源
最近更新 更多