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

Des*_*awn 91 landscape ipad uipopovercontroller ios6

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

Sni*_*ers 92

在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;
}
Run Code Online (Sandbox Code Playgroud)

如果它不是您自己的视图(例如iPhone上的GameCenter),则需要确保您的.plist支持纵向模式.您还需要确保UIApplicationDelegate支持以纵向模式显示的视图.您可以通过编辑.plist然后覆盖UIApplicationDelegate上的supportedInterfaceOrientation来完成此操作:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}
Run Code Online (Sandbox Code Playgroud)

  • 在iOS7中不起作用.OP发布的错误相同. (5认同)
  • 或者只使用UIInterfaceOrientationMaskAll. (4认同)
  • `UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight`等于`UIInterfaceOrientationMaskAllButUpsideDown``UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight`等于`UIInterfaceOrientationMaskLandscape` (3认同)

Dr.*_*iji 65

在花了很多时间寻找避免子类化和添加大量代码的方法后,这是我的一行代码解决方案.

创建一个新的UIImagePickerController的类别并添加

-(BOOL)shouldAutorotate{
    return NO;
}
Run Code Online (Sandbox Code Playgroud)

这就是所有人!

  • 在iOS7中不起作用,甚至没有调用类别方法(虽然包含在pch中) (3认同)
  • 这似乎是最好的解决方案. (2认同)
  • 作品!这太棒了,它真的帮助了我.谢谢Luiji博士!! (2认同)

Jac*_*rse 43

还有另一种情况可能会出现此错误消息.我找了几个小时,直到找到问题.阅读它之后,这个帖子非常有用.

如果您的主视图控制器旋转到横向和调用应显示在纵向当你的代码看起来像这样可能发生此错误消息的自定义子视图控制器:

- (NSUInteger)supportedInterfaceOrientations {

    return UIInterfaceOrientationPortrait;
}
Run Code Online (Sandbox Code Playgroud)

这里的陷阱是xcode的intellisense建议"UIInterfaceOrientationPortrait",我并不关心它.乍一看,这似乎是正确的.

正确的面具被命名

UIInterfaceOrientationMaskPortrait
Run Code Online (Sandbox Code Playgroud)

请注意小中缀"Mask",否则您的子视图最终会出现异常和上面提到的错误消息.

新的枚举有点移位.旧的枚举返回无效值!

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

解决方案是:

- (BOOL)shouldAutorotate {

    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {

    // ATTENTION! Only return orientation MASK values
    // return UIInterfaceOrientationPortrait;

    return UIInterfaceOrientationMaskPortrait;
} 
Run Code Online (Sandbox Code Playgroud)

在迅速使用

override func shouldAutorotate() -> Bool {

    return true
}

override func supportedInterfaceOrientations() -> Int {

    return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}
Run Code Online (Sandbox Code Playgroud)

  • 因为同样的原因发生在我身上 (5认同)
  • 你好我的一天;) (2认同)

小智 22

在仅景观应用程序中呈现图像选择器时,我遇到了类似的问题.正如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
Run Code Online (Sandbox Code Playgroud)

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


Rob*_*bby 11

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

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

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


小智 6

我有同样的问题,这个答案/sf/answers/876674151/适合我.不知道是否有更优雅的解决方案.

我的代码:

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];
Run Code Online (Sandbox Code Playgroud)