ChatGPT解决这个技术问题 Extra ChatGPT

支持的方向与应用程序没有共同的方向,并且 shouldAutorotate 返回 YES'

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

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

S
Snickers

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

.plist(或目标摘要屏幕)您的 UIApplicationDelegate 正在显示的 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 添加纵向支持。谢谢你。
d
dandan78

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

新建一个 UIImagePickerController 的类别并添加

-(BOOL)shouldAutorotate{
    return NO;
}

这就是所有的人!


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

在另一种情况下,可能会出现此错误消息。我找了好几个小时才发现问题。这篇文章在阅读了几次之后非常有帮助。

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

- (NSUInteger)supportedInterfaceOrientations {

    return UIInterfaceOrientationPortrait;
}

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

正确的面具被命名

UIInterfaceOrientationMaskPortrait

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

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

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

解决方案是:

- (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 后它崩溃了。现在这个解决方案解决了我的问题。多谢 :-)
s
shawnwall

在仅横向应用程序中显示图像选择器时,我遇到了类似的问题。根据 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 之前添加这些行是最简单的。


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

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

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

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


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

我遇到了同样的问题,这个答案 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
S
Suhail Bhat
- (BOOL)shouldAutorotate {
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

This removes the crash.

d
derbs

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

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

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


E
EquiAvia Tech

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

@interface MyImagePickerController ()

@end

@implementation MyImagePickerController

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

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


H
Helge Staedtler

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

其他解决方案可能已经奏效。但是,将 SDK for iOS 8.x 编译为部署在 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

z
zombie

斯威夫特 3

let imagePicker = UIImagePickerController()

imagePicker.modalPresentationStyle = .popover
imagePicker.popoverPresentationController?.sourceView = sender // you can also pass any view 

present(imagePicker, animated: true)

G
Gal Blank

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

override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .portrait
}

open override var shouldAutorotate: Bool {
    return false
}

只有在非调试版本中运行我的应用程序时,我才会遇到这个问题。这个答案为我解决了这个问题。
I
Itachi

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

UIPageViewControllerDelegate 上的更多代码背景,仅供大家参考。

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

a
atereshkov

我刚刚解决了 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
    }
}

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


关注公众号,不定期副业成功案例分享
关注公众号

不定期副业成功案例分享

领先一步获取最新的外包任务吗?

立即订阅