9

I have an app that fully support rotation. I am modally adding a UIImagePickerController for which there is no support for UIInterfaceOrientationLandscape and I cannot get the controller to stay in portrait.

In other words, I need to disable rotation for the UIImagePickerController so it stays in portrait, without removing rotation for the rest of my app. this seems basic, but I can't seem to locate it. How can I prevent this rotation?



UPDATE

As suggested, I tried subclassing with the following code:

@interface UAImagePickerController : UIImagePickerController {
}
@end

@implementation UAImagePickerController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return UIDeviceOrientationIsPortrait(toInterfaceOrientation);
}
@end

The line is not being hit at all with a breakpoint… i think there must be something funky about the UIImagePickerView

coneybeare
  • 33,113
  • 21
  • 131
  • 183
  • For the record, I just noticed that the imagepickerview in my iPhone app rotates even though the app doesn't support landscape. So there seems to be no "legal" way to disable rotation of the camera view? Sounds like a radar, or at least a change request. – DaGaMs Jun 25 '12 at 14:37

4 Answers4

-1

I created my UIImagePickerController inside a UIViewController with the respective shouldAutorotateToInterfaceOrientation method implemented and presented it like this

[self.view addSubview:myImagePicker.view];
[targetVC.navigationController presentModalViewController:self animated:YES];

Hope that helps.

Juan de la Torre
  • 1,297
  • 9
  • 19
-2

Subclass UIImagePickerController and have that new class implement shouldAutorotateToInterfaceOrientation: to make it rotate to portrait only, something like this:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
Shaggy Frog
  • 27,575
  • 16
  • 91
  • 128
  • As an addendum to your answer, you should always (now adays) use the `UIDeviceOrientationIsLandscape` and `UIDeviceOrientationIsPortrait` macros instead. For instance, iPad apps that don't support at least 2 orientations (i.e., portrait up and portrait down or landscape left and landscape right) will be rejected. These macros return `YES` on either landscape orientation or either portrait orientation, depending on which you use. – jer Dec 05 '10 at 03:16
  • @jer I've submitted an iPad app that only supported 1 orientation and it was not rejected, so I'm not sure from where you are drawing that assertion. As for the code itself, it's what Xcode autogenerates when you create a new view controller class, so if you are certain it is prone to a rejection, you should file a bug with Apple. – Shaggy Frog Dec 05 '10 at 03:41
  • The HIG says that apps must support 2 orientations at a minimum. It does however say that some applications (games) may not be able to support multiple orientations. However, there have been games rejected for not supporting multiple orientations. So you plow your own path when you only support one. – jer Dec 05 '10 at 03:48
  • Either way, its not being triggered like a normal subclass would. Apple says that the UIImagePickerController only supports portrait so your arguments are moot here. – coneybeare Dec 05 '10 at 03:48
  • I am using some special embedded navigationControllers and had some issues in linking them up properly. I can now prevent it from rotating but am still trying to get it to present portrait all the time, even if its parent is landscape. it is troublesome with just this fickle UIImagePickerController. – coneybeare Dec 06 '10 at 06:26
  • @coneybeare Looks like I got the -1 driveby, too. Anyway, trying to force the orientation in the case you described may not be HIG-compliant. For example, if your user has the device in landscape and then does an action to raise the image picker, and the image picker is in portrait, that's going to be annoying to the user, and they'll wonder, why can't this image picker be in landscape like everything else? I think you may need to re-examine your design decisions with respect to this user case scenario. – Shaggy Frog Dec 06 '10 at 06:46
  • The problem stems from Apple's lack of support for a landscape image picker. I am not about to remove all rotation from my app to support the picker. I decided to use a try-wrapped private api call on the calling controller to force it into portrait, where the subsequent call to the image picker is then presented correctly. – coneybeare Dec 06 '10 at 16:18
  • @coneybeare What did you change to finally prevent it from rotating? I've been unsuccessful w/ the same subclassing technique... – kamens Mar 26 '11 at 20:32
-4

You should override _isSupportedInterfaceOrientation:, and not the shouldAutorotateToInterfaceOrientation:

iPad 2 UIImagePickerController camera auto-rotation driving me mad!

Community
  • 1
  • 1
Dmitry
  • 1,035
  • 2
  • 14
  • 27
  • This is a private method and is unsupported by Apple. It is not a recommended solution and you will probably be rejected for using it. – coneybeare Jun 20 '11 at 13:46
  • Thanks cap!!! But it's work. Will it be rejected or not depends on your implementation. There is a way to confuse anyone who will check it ;) – Dmitry Jun 22 '11 at 07:55
-4

Just drop this in above your ViewControllers @implementation block (in the .m file)

@implementation UIImagePickerController (portrait)

- (BOOL)_isSupportedInterfaceOrientation:(UIDeviceOrientation)orientation
{
    return UIDeviceOrientationIsPortrait(orientation);
}

@end
Sebastian Bean
  • 142
  • 2
  • 8
  • 1
    Private methods violate the Apple developer agreement, are dangerous and should not be placed into any production environment. – coneybeare Aug 24 '11 at 23:39