【问题标题】:IOS 6 Orientations - Only One Viewcontroller supports Landscape & Portrait orientation - else ONLY PortraitIOS 6 方向 - 只有一个 Viewcontroller 支持横向和纵向方向 - 否则只支持纵向
【发布时间】:2013-06-07 16:04:00
【问题描述】:

正如长标题所暗示的那样,我最近一直在为 IOS 6 Orientations 苦苦挣扎。 我正在处理一个仅基于 iPhone 的当前项目,几乎所有的视图控制器都只支持纵向(默认方向)。 我有这个视图控制器虽然我想给多个方向作为唯一的一个,独立的。我该如何正确地做到这一点?这是我的方法。干杯'

在设置中 - 选择除纵向底部向上之外的所有方向。

  • 在我想给出多个方向的 Viewcontroller 中 - 嵌入了此代码

    -(NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
    
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    {
        return UIInterfaceOrientationPortrait;
    }
    
    -(BOOL)shouldAutorotate
    {
        return YES;
    }
    

对于其余的视图控制器 - 支持一种方向(默认方向)纵向:

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

-(BOOL)shouldAutorotate
{
    return YES;
}

它不起作用。似乎没有调用方法 - 但项目和构建只是坚持设置中的选定方向。为什么这不起作用!

任何帮助将不胜感激。

【问题讨论】:

  • 如果你想知道方法是否被调用然后NSLog(@"whateverMethodWasCalled in myViewController")
  • 一点帮助都没有,你知道是否可以覆盖视图控制器中“设置”中设置的方向吗?
  • 设置定义了您的应用支持的方向。您的视图控制器在supportedInterfaceOrientations 中说明了他们支持的内容。应用程序设置 (.plist) 和您在视图控制器的 supportedInterfaceOrientations 中定义的方向重叠的任何方向都是您的视图将自动旋转到的方向。
  • 感谢@Unicorn 的回复,是的,我想知道我的 shouldautorotate & supportedInterfaceOrientations 中的方向方法是否被正确调用。

标签: ios xcode uiviewcontroller orientation


【解决方案1】:

您可以阅读:

Multiple Orientations

这就是我的工作:

在所有视图控制器中:

#pragma mark Orientation handling

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
    }

    -(BOOL)shouldAutorotate
    {
        return YES;
    }

    -(NSUInteger)supportedInterfaceOrientations
    {
        return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
    }

唯一的视图控制器:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return YES;
}

希望这会有所帮助..

【讨论】:

  • 您将interfaceOrientationUIInterfaceOrientationUIInterfaceOrientationMaskAllButUpsideDown 进行比较,UIInterfaceOrientationMask。发现问题了吗?
  • 感谢您的回复@lakesh 赞赏,但在 ios 6 及更高版本中不支持 shouldAutorotateToInterfaceOrientation 方法。虽然我会尝试代码并让你知道我发现了什么;)
  • 如果你使用的是ios6及以上版本,请使用preferredInterfaceOrientationForPresentation
【解决方案2】:

试试这个[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];,你需要在构建阶段为这个文件设置-fno-objc-arc

【讨论】:

  • 私有 API,糟糕的主意。
【解决方案3】:

-supportedInterfaceOrientations 应该返回一个掩码,在你的情况下:

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

【讨论】:

    【解决方案4】:

    您需要为要纵向显示的视图创建一个UINavigationController 子类。从 iOS 6 开始,视图控制器仅查看父控制器或根控制器以进行旋转处理而不是视图控制器本身,您正在根据您的描述进行操作。

    在导航控制器子类.m中:

    // Older versions of iOS (deprecated) if supporting iOS < 5
     - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation    {
    return UIInterfaceOrientationIsPortrait(toInterfaceOrientation);
    }
    
    // >iOS6
     - (BOOL)shouldAutorotate {
    return YES;
     }
    
     // >iOS6
     - (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
    }
    

    Allow autorotation on just one view controller

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-01
      • 2018-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-06
      • 1970-01-01
      • 2013-09-18
      相关资源
      最近更新 更多