我的应用程序(iPad; iOS 6)是仅横向应用程序,但是当我尝试使用UIPopoverController显示照片库时,会引发此错误:
Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES.
我尝试更改很多代码,但没有运气。
我的应用程序(iPad; iOS 6)是仅横向应用程序,但是当我尝试使用UIPopoverController显示照片库时,会引发此错误:
Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES.
我尝试更改很多代码,但没有运气。
Answers:
在IOS6中,您在三个地方都支持界面方向:
如果遇到此错误,则很可能是因为您在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
在花了很多时间寻找避免子类化和添加大量代码的方法之后,这是我的单行代码解决方案。
创建一个新的UIImagePickerController的类别并添加
-(BOOL)shouldAutorotate{
return NO;
}
那是所有人!
在另一种情况下,可能会出现此错误消息。我花了好几个小时才发现问题。阅读几次后,此线程非常有帮助。
如果将主视图控制器旋转为横向,并且您调用一个应以纵向显示的自定义子视图控制器,则在代码如下所示时可能会发生此错误消息:
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationPortrait;
}
这里的陷阱是xcode的intellisense建议“ UIInterfaceOrientationPortrait”,我对此并不在意。乍一看,这似乎是正确的。
右边的面具叫
UIInterfaceOrientationMaskPortrait
请注意小前缀“ Mask”,否则您的子视图将最终出现异常和上面提到的错误消息。
新的枚举已移位。旧的枚举返回无效值!
(在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)
}
在仅横向应用中显示图像选择器时,我遇到了类似的问题。正如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之前添加这些行是最简单的。
我在代码中遇到了相同的错误消息。我发现了,这是Apple报告的错误:
https://devforums.apple.com/message/731764#731764
他的解决方案是在AppDelegate中修复它。我实现了它,对我有用!
我有同样的问题,这个答案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。
iOS 8-您可以使用UIModalPresentationPopover而无需任何技巧即可在弹出窗口中显示。不理想,但总比没有好。
imagePicker.modalPresentationStyle = UIModalPresentationPopover;
imagePicker.popoverPresentationController.sourceView = self.view;
imagePicker.popoverPresentationController.sourceRect = ((UIButton *)sender).frame;
编辑:也许尝试不同的UIModalPresentationStyles-也许更多的可以在横向使用。
- (BOOL)shouldAutorotate {
return NO;
}
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
This removes the crash.
解决我的问题的另一个选项是创建UIImagePickerController的子类并覆盖以下方法
@interface MyImagePickerController ()
@end
@implementation MyImagePickerController
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
使用它代替UIImagePickerController,一切正常。
创建类别确实有助于修复此错误。并且不要忘记导入您创建的类别。这会将缺少的方法添加到UIImagePickerController中,并且在iOS 6上,它将仅在文档说明btw时才限制只能在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
在隐式转换UIInterfaceOrientationPortrait
UIInterfaceOrientationMaskPortrait
为返回值时遇到了崩溃问题。
有关更多代码背景UIPageViewControllerDelegate
,仅供大家参考。
-(UIInterfaceOrientationMask)pageViewControllerSupportedInterfaceOrientations:
(UIPageViewController *)pageViewController
{
# return UIInterfaceOrientationPortrait; # wrong
return UIInterfaceOrientationMaskPortrait; # correct
}
我刚刚解决了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
}
}
谢谢你们的回答。我刚刚剪切了工作的代码并对其进行了重构。