私のアプリ (iPad; iOS 6) は横向き専用のアプリケーションですが、UIPopoverController を使用してフォト ライブラリを表示しようとすると、次のエラーがスローされます。
Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES.
多くのコードを変更しようとしましたが、うまくいきませんでした。
14 に答える
IOS6 では、次の 3 つの場所でインターフェイスの向きがサポートされています。
- .plist (またはターゲットの概要画面)
- あなたのUIApplicationDelegate
- 表示されている UIViewController
このエラーが発生する場合は、UIPover に読み込んでいるビューがポートレート モードのみをサポートしている可能性があります。これは、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;
}
サブクラス化と大量のコードの追加を回避する方法を探すのに多くの時間を費やした後、これが私の 1 行のコード ソリューションです。
新しい UIImagePickerController のカテゴリを作成して追加します
-(BOOL)shouldAutorotate{
return NO;
}
それはすべての人々です!
このエラー メッセージが表示される別のケースがあります。問題が見つかるまで何時間も探していました。このスレッドは、数回読んだ後、非常に役に立ちました。
メイン ビュー コントローラーが横向きに回転され、縦向きで表示されるカスタム サブ ビュー コントローラーを呼び出した場合、コードが次のような場合に、このエラー メッセージが発生する可能性があります。
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationPortrait;
}
ここでの罠は、xcode のインテリセンスが「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];
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 では、ドキュメンテーションに記載されているように、Portrait でのみ機能するように制限されます。
他の解決策が機能した可能性があります。しかし、iOS 6.1 に展開するためにコンパイルされた iOS 8.x 用の SDK では、これが進むべき道のようです。
.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
}