【问题标题】:Cannot rotate interface orientation to portrait upside down无法将界面方向旋转为纵向颠倒
【发布时间】:2012-12-13 00:37:56
【问题描述】:

在 iPhone 模拟器和 iPhone 3GS (iOS 6) 上,我都无法将方向设置为纵向倒置。我只有一个 ViewController。我已经在其中添加了这段代码:

-(BOOL) shouldAutorotate{
 return YES; 
}

-(UIInterfaceOrientation) preferredInterfaceOrientationForPresentation{
 return UIInterfaceOrientationPortraitUpsideDown;
}

- (NSUInteger)supportedInterfaceOrientations{
 return UIInterfaceOrientationPortrait | UIInterfaceOrientationPortraitUpsideDown;
}

-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
  if (toInterfaceOrientation==UIInterfaceOrientationPortrait || toInterfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) {
    return YES;
  }
 return NO;
}

我也将此添加到 AppDelegate.m:

-(NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    return UIInterfaceOrientationPortrait | UIInterfaceOrientationPortraitUpsideDown;
}

我还检查了 plist 文件,两个方向都存在。在我的故事板上,Simulated Metrics Orientation 设置为 Inferred。我在 applicationDidFinishLaunchingWithOptions 中什么都不做,除了返回 YES。我刚刚在这个 ViewController 中添加了一个 UIImageView。是否有可能使这种定向工作?

【问题讨论】:

  • supportedInterfaceOrientationsForWindow: 应该返回 UIInterfaceOrientationMask。

标签: iphone ios6 storyboard portrait interface-orientation


【解决方案1】:

supportedInterfaceOrientations 返回一个NSUInteger。它返回视图控制器支持的所有界面方向,一个界面方向值的掩码。

您需要返回UIInterfaceOrientationMask枚举中定义的值,如下所示:

- (BOOL)shouldAutorotate {
    return YES;
}

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

【讨论】:

  • 非常感谢!起初我以为我做了完全一样的事情,但后来我注意到了正确的名字)
  • 我必须在目标的信息部分设置它以使其完全工作。
  • 现在有一个组合的 UIInterfaceOrientationMaskAll
【解决方案2】:

我知道有一个有效的答案,但对于仍然遇到同样问题的其他人:

我遇到了一个与 Xcode 相关的类似问题:尽管在所有情况下都将 YES 返回到 - (BOOL) shouldAutorotateToInterfaceOrientation:,但不支持纵向旋转。结果是我的目标项目编辑器的摘要窗口中启用了“支持的界面方向”:

上面的“iPhone/iPad 部署信息”选项没有这样做,尽管我只使用 iPhone sim,但似乎是“iPad 部署信息”部分控制了模拟器将做什么。一旦我在该部分中启用了相同的方向,iPhone 模拟就会工作,并且我能够测试模拟器旋转时发生的情况....

【讨论】:

  • 只是我还是 XCode 中的 Landscape Left 和 Landscape Right 图形不正确? Apple 文档 (developer.apple.com/library/ios/#documentation/UIKit/Reference/…) 中的横向左侧: UIDeviceOrientationLandscapeLeft = 设备处于横向模式,设备保持直立,主页按钮位于右侧。上面的横向左侧图形显示左侧的主页按钮。在设备记录方向上对此进行了测试,果然,横向左侧应该有右侧的主页按钮。
  • 所以,UIDeviceOrientationLandscapeLeft 和 Right 是 UIInterfaceOrientationLandscapeLeft 和 Right 的对立面。将上面的文档与 UIInterfaceOrientation 的文档进行比较:developer.apple.com/library/ios/#documentation/UIKit/Reference/…
  • 好地方!可能是因为 phobe 和 cg 界面有不同的锚点,因此相对于不同的原点旋转......?但是为什么呢!?
  • @Todd 的想法更像是用户界面旋转必须抵消设备旋转。如果当您向左旋转设备时用户界面会向左旋转,您最终会看到一个颠倒的用户界面。设备旋转 90 度加上用户界面旋转 90 度等于 180 度(从您开始的位置倒置)。要抵消 90 度设备旋转,您必须在用户界面上执行 -90 度。这样,旋转前的上升在旋转后仍然上升为 90+(-90) = 0。
【解决方案3】:

为此我尝试了很多方法。它似乎只是一种工作方式,但通过应用程序全局运行,不仅适用于特定的视图控制器。

首先,您必须在目标常规设置中检查设备方向的颠倒。

然后,在导航控制器扩展中,覆盖 supportedInterfaceOrientations 方法

extension UINavigationController {
    override public func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return .All
    }
}

【讨论】:

    猜你喜欢
    • 2018-03-16
    • 1970-01-01
    • 2012-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    相关资源
    最近更新 更多