In IOS6 you have supported interface orientations in three places:
The .plist (or Target Summary Screen)
Your UIApplicationDelegate
The UIViewController that is being displayed
If you are getting this error it is most likely because the view you are loading in your UIPopover only supports portrait mode. This can be caused by Game Center, iAd, or your own view.
If it is your own view, you can fix it by overriding supportedInterfaceOrientations on your UIViewController:
- (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;
}
If it is not your own view (such as GameCenter on the iPhone), you need to make sure your .plist supports portrait mode. You also need to make sure your UIApplicationDelegate supports views that are displayed in portrait mode. You can do this by editing your .plist and then overriding the supportedInterfaceOrientation on your UIApplicationDelegate:
I had a similar issue when presenting the image picker in a landscape only app. As suggested by Dr. Luiji's, I added the following category at the beginning of my controller.
// 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
It's easiest to add these lines just before the @implementation of your ViewController .m file.
There is another case this error message may appear. I was searching for hours until I found the problem. This thread was very helpful after reading it a couple of times.
If your main view controller is rotated to landscape orientation and you invoke a custom sub view controller which should be displayed in portrait orientation this error message can happen when your code looks like this:
The trap here was xcode's intellisense suggested "UIInterfaceOrientationPortrait" and I didn't care about it. At the first glance this seemed to be correct.
The right mask is named
UIInterfaceOrientationMaskPortrait
Be aware of the small infix "Mask", else your subview will end up with an exception and the mentioned error message above.
The new enums are bit shifted. The old enums return invalid values!
(in UIApplication.h you can see the new declaration: UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait) )
Creating a category is really helpful to fix this bug. And do not forget to import your created category. This will add the missing method to UIImagePickerController and on iOS 6 it will restrict it to work in Portrait only as the documentation states btw.
The other solutions may have worked. But with SDK for iOS 8.x compiled to deploy on iOS 6.1 this seems the way to go.
let imagePicker = UIImagePickerController()
imagePicker.modalPresentationStyle = .popover
imagePicker.popoverPresentationController?.sourceView = sender // you can also pass any view
present(imagePicker, animated: true)
Swift 4 and up assuming the whole app is in landscape and you need to present a single controller in portrait.
In the view controller that has to be portrait add the following :
override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
open override var shouldAutorotate: Bool {
return false
}