【问题标题】:Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES'支持的方向与应用程序没有共同的方向,并且 shouldAutorotate 返回 YES'
【发布时间】:2012-09-14 11:17:33
【问题描述】:

我的应用程序(iPad;iOS 6)是一个仅限横向的应用程序,但是当我尝试使用 UIPopoverController 来显示照片库时,它会引发以下错误: Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES.我已经尝试更改很多代码,但我没有运气。

【问题讨论】:

  • 你应该接受一个答案。有很多人需要帮助,对于遇到相同问题的其他人来说,当您为您的问题标记正确答案时,它会有所帮助!
  • 不!在 iOS 7 上没有解决方案 :( (headbang)

标签: ipad uipopovercontroller landscape ios6


【解决方案1】:

Swift 4 及更高版本假设整个应用程序处于横向状态,并且您需要以纵向显示单个控制器。 在必须是纵向的视图控制器中添加以下内容:

override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .portrait
}

open override var shouldAutorotate: Bool {
    return false
}

【讨论】:

  • 我只有在非调试版本中运行我的应用程序时才会遇到这个问题。这个答案为我解决了这个问题。
【解决方案2】:

我刚刚解决了 Swift 4.x

的问题
override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    configureVideoOrientation()
}

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)
    coordinator.animate(alongsideTransition: nil, completion: { [weak self] _ in
        self?.configureVideoOrientation()
    })
}

private func configureVideoOrientation() {
    guard let previewLayer = self.previewLayer, let connection = previewLayer.connection else { return }
    if connection.isVideoOrientationSupported {
        let orientation = UIApplication.shared.statusBarOrientation
        switch (orientation) {
        case .portrait:
            previewLayer.connection?.videoOrientation = .portrait
        case .landscapeRight:
            previewLayer.connection?.videoOrientation = .landscapeRight
        case .landscapeLeft:
            previewLayer.connection?.videoOrientation = .landscapeLeft
        case .portraitUpsideDown:
            previewLayer.connection?.videoOrientation = .portraitUpsideDown
        default:
            previewLayer.connection?.videoOrientation = .portrait
        }

        previewLayer.frame = self.view.bounds
    }
}

也谢谢你们的回答。我刚刚剪切了工作代码并简单地对其进行了重构。

【讨论】:

    【解决方案3】:

    我在将UIInterfaceOrientationPortrait隐式转换为UIInterfaceOrientationMaskPortrait作为返回值时遇到了这个崩溃问题。

    更多代码背景见UIPageViewControllerDelegate,仅供参考。

     -(UIInterfaceOrientationMask)pageViewControllerSupportedInterfaceOrientations:
    (UIPageViewController *)pageViewController
    {
        # return UIInterfaceOrientationPortrait;    # wrong
        return UIInterfaceOrientationMaskPortrait;  # correct
    }
    

    【讨论】:

      【解决方案4】:

      斯威夫特 3

      let imagePicker = UIImagePickerController()
      
      imagePicker.modalPresentationStyle = .popover
      imagePicker.popoverPresentationController?.sourceView = sender // you can also pass any view 
      
      present(imagePicker, animated: true)
      

      【讨论】:

        【解决方案5】:

        在另一种情况下可能会出现此错误消息。我找了好几个小时才发现问题。看了几遍,这个帖子很有帮助。

        如果您的主视图控制器旋转到横向,并且您调用了一个应该以纵向显示的自定义子视图控制器,则当您的代码如下所示时,可能会出现此错误消息:

        - (NSUInteger)supportedInterfaceOrientations {
        
            return UIInterfaceOrientationPortrait;
        }
        

        这里的陷阱是 xcode 的智能感知建议“UIInterfaceOrientationPortrait”,我不在乎。乍一看,这似乎是正确的。

        正确的掩码命名

        UIInterfaceOrientationMaskPortrait
        

        注意小中缀“掩码”,否则您的子视图将出现异常和上面提到的错误消息。

        新的枚举被移位。旧枚举返回无效值!

        (在 UIApplication.h 中你可以看到新的声明:UIInterfaceOrientationMaskPortrait = (1 )

        解决办法是:

        - (BOOL)shouldAutorotate {
        
            return YES;
        }
        
        - (NSUInteger)supportedInterfaceOrientations {
        
            // ATTENTION! Only return orientation MASK values
            // return UIInterfaceOrientationPortrait;
        
            return UIInterfaceOrientationMaskPortrait;
        } 
        

        快速使用

        override func shouldAutorotate() -> Bool {
        
            return true
        }
        
        override func supportedInterfaceOrientations() -> Int {
        
            return Int(UIInterfaceOrientationMask.Portrait.rawValue)
        }
        

        【讨论】:

        • 出于同样的原因发生在我身上
        • 我希望苹果将返回类型设为 UIInterfaceOrientationMask,这样需要返回的内容就更明显了。
        • Jackpearse 的答案看起来是正确的。但是当查看 info.plist 时,有 UISupportedInterfaceOrientations 和 UIInterfaceOrientationPortrait、UIInterfaceOrientationLandscapeLeft、UIInterfaceOrientationLandscapeRight。这里没有面具
        • @onmyway:没错,plist 值没有改变。在之前的 iOS 版本中,Apple 在代码中使用了“非”掩码值。这是一些遗留的东西。我假设苹果现在正在内部对 plist 值进行位移。
        • 我的应用程序过去在 Xcode 6.2 的 iPad 模拟器上运行时没有这个崩溃问题。几天前我升级到 Xcode 6.3 后它崩溃了。现在这个解决方案解决了我的问题。谢谢很多:-)
        【解决方案6】:

        创建一个类别对于修复这个错误非常有帮助。并且不要忘记导入您创建的类别。这会将缺少的方法添加到 UIImagePickerController 中,并且在 iOS 6 上,它将限制它仅在文档状态下在 Portrait 中工作。

        其他解决方案可能已经奏效。但是,将适用于 iOS 8.x 的 SDK 编译为部署在 iOS 6.1 上,这似乎是可行的方法。

        .h 文件:

        #import <UIKit/UIKit.h>
        
        @interface UIImagePickerController (iOS6FIX)
        
        - (BOOL) shouldAutorotate;
        - (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation;
        
        @end
        

        .m 文件:

        #import "UIImagePickerController+iOS6FIX.h"
        
        @implementation UIImagePickerController (iOS6FIX)
        
        - (BOOL) shouldAutorotate {
            return NO;
        }
        
        - (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation {
            return UIInterfaceOrientationPortrait;
        }
        
        @end
        

        【讨论】:

          【解决方案7】:

          iOS 8 - 您可以使用 UIModalPresentationPopover 而无需任何技巧来显示在弹出窗口中。不理想,但总比没有好。

          imagePicker.modalPresentationStyle = UIModalPresentationPopover;
          imagePicker.popoverPresentationController.sourceView = self.view;
          imagePicker.popoverPresentationController.sourceRect = ((UIButton *)sender).frame;
          

          编辑:也许尝试不同的 UIModalPresentationStyles - 也许更多会在横向工作。

          【讨论】:

            【解决方案8】:
            - (BOOL)shouldAutorotate {
                return NO;
            }
            
            -(NSUInteger)supportedInterfaceOrientations {
                return UIInterfaceOrientationMaskPortrait;
            }
            
            This removes the crash.
            

            【讨论】:

              【解决方案9】:

              在花费大量时间寻找避免子类化和添加大量代码的方法之后, 这是我的单行代码解决方案。

              新建一个UIImagePickerController的类别并添加

              -(BOOL)shouldAutorotate{
                  return NO;
              }
              

              就是这样!

              【讨论】:

              • 也为我工作,尚未完全测试,但到目前为止似乎可以解决问题。
              • 这似乎是最好的解决方案。
              • 有效!这太棒了,它真的在紧要关头帮助了我。谢谢路易吉博士!!
              • 不错。在 iOS 6 上为我工作。您也可以在类别中进行配置以获得完全的灵活性。
              • 在iOS7中不起作用,甚至没有调用category方法(虽然包含在pch中)
              【解决方案10】:

              在仅横向应用程序中显示图像选择器时,我遇到了类似的问题。根据 Luiji 博士的建议,我在控制器的开头添加了以下类别。

              // This category (i.e. class extension) is a workaround to get the
              // Image PickerController to appear in landscape mode.
              @interface UIImagePickerController(Nonrotating)
              - (BOOL)shouldAutorotate;
              @end
              
              @implementation UIImagePickerController(Nonrotating)
              
              - (BOOL)shouldAutorotate {
                return NO;
              }
              @end
              

              在您的 ViewController .m 文件的 @implementation 之前添加这些行是最简单的。

              【讨论】:

              • 我有一个只有肖像的应用程序!我想以横向模式显示相机。有什么解决办法??????
              • Trausti 没有工作?我有同样的问题,我尝试了你的代码,但仍然出现相同的异常 *** 由于未捕获的异常“UIApplicationInvalidInterfaceOrientation”而终止应用程序,原因:“支持的方向与应用程序没有共同的方向,并且 shouldAutorotate 返回是..... ...我还在 UIimagePickerView 类别方法 shouldAutorotate 到达那里设置了一个断点并返回 no 但仍然给出此异常 ....我的应用程序是 Landscape only 应用程序...请帮助我
              • 还可以强制 SKStoreProductViewController 在 iOS 8 中整个应用为横向时以纵向显示。谢谢。
              【解决方案11】:

              我在代码中遇到了相同的错误消息。我发现了这个,这是 Apple 报告的一个错误:

              https://devforums.apple.com/message/731764#731764

              他的解决方案是在 AppDelegate 中修复它。我实现了它,它对我有用!

              【讨论】:

              • 顺便说一下,我最初是在:stackoverflow.com/questions/12522491/…
              • 这是正确的答案,直接来自 Apple,然后我只需要按照相同的说明进行操作,但在针对 iOS 8 的应用程序中使用 Swift 进行操作。一切都很好。 AppDelegate 将supportedInterfaceOrientationsForWindow 设置为横向和纵向,每个单独的swift 文件将覆盖func supportedInterfaceOrientations 设置为横向,因此视图不会旋转,但是当需要加载纵向视图(在我的情况下为SKStoreProductViewController)时,它可以工作!
              • 天啊,有这么多建议的解决方案,而这一个到目前为止还没有最高点,但它确实是正确的。在 iOS 10 和 9 上进行了测试。没有其他工作。
              【解决方案12】:

              解决我的问题的另一个选项是创建 UIImagePickerController 的子类并覆盖以下方法

              @interface MyImagePickerController ()
              
              @end
              
              @implementation MyImagePickerController
              
              - (NSUInteger)supportedInterfaceOrientations {
                  return UIInterfaceOrientationMaskLandscape;
              }
              

              使用它而不是 UIImagePickerController 并且一切正常。

              【讨论】:

                【解决方案13】:

                在 IOS6 中,您在三个地方支持界面方向:

                1. .plist(或目标摘要屏幕)
                2. 你的 UIApplicationDelegate
                3. 正在显示的 UIViewController

                如果您收到此错误,很可能是因为您在 UIPopover 中加载的视图仅支持纵向模式。这可能是由 Game Center、iAd 或您自己的观点引起的。

                如果是你自己的视图,你可以通过覆盖 UIViewController 上的 supportedInterfaceOrientations 来修复它:

                - (NSUInteger) supportedInterfaceOrientations
                {
                     //Because your app is only landscape, your view controller for the view in your
                     // popover needs to support only landscape
                     return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
                }
                

                如果不是你自己的视图(比如 iPhone 上的 GameCenter),你需要确保你的 .plist 支持竖屏模式。您还需要确保您的 UIApplicationDelegate 支持以纵向模式显示的视图。您可以通过编辑 .plist 然后覆盖 UIApplicationDelegate 上的 supportedInterfaceOrientation 来做到这一点:

                - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
                {
                    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
                }
                

                【讨论】:

                • UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight 等于 UIInterfaceOrientationMaskAllButUpsideDown UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight 等于 UIInterfaceOrientationMaskLandscape
                • 或者只使用 UIInterfaceOrientationMaskAll。
                • 完美。即使“仅纵向”弹出框可以在横向中工作,但它不能以模态方式呈现仅横向视图控制器。
                • 不!解决方案不适用于 iOS 7 :(。stackoverflow.com/questions/23005351/…
                • 在 iOS 8 上工作。不过,必须为 plist 添加纵向支持。谢谢。
                【解决方案14】:

                我遇到了同样的问题,这个答案https://stackoverflow.com/a/12523916 对我有用。想知道是否有更优雅的解决方案。

                我的代码:

                UIImagePickerController  *imagePickerController = [[NonRotatingUIImagePickerController alloc] init];
                imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                
                UIPopoverController  *popoverVC = [[UIPopoverController alloc] initWithContentViewController:imagePickerController];    
                
                [popoverVC presentPopoverFromRect:frame   // did you forget to call this method?
                                           inView:view
                         permittedArrowDirections:UIPopoverArrowDirectionAny
                                         animated:YES];
                

                【讨论】:

                • 我试过这样做,但是当我单击按钮调用代码但没有显示任何内容时,我也尝试将 @interface NonRotatingUIImagePickerController : UIImagePickerController @end @implementation NonRotatingUIImagePickerController - (BOOL)shouldAutorotate { return NO; } @end 添加到代码中,但我的代码无法检测到 NonRotatingUIImagePickerController。
                • 没关系,它现在检测到 NonRotatingUIImagePickerController 但什么都没有显示... @poetowen
                猜你喜欢
                • 2014-11-19
                • 1970-01-01
                • 1970-01-01
                • 2013-01-05
                • 1970-01-01
                • 2015-11-17
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多