Howto set up shouldAutorotateToInterfaceOrientation
Now here I have a bone to pick with many iOS Developers!
Please set up your UIViewController’s
"- (BOOL)shouldAutorotateToInterfaceOrientation:"
correctly, so you support both your Orientation and its UpsideDown-Version. Yes, it’s fine to just support Portrait or Landscape mode, but please support both directions!
You see UIInterfaceOrientation has 4 directions:
typedef enum {
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
} UIInterfaceOrientation;
Now I’m fine if you just want to support Portrait or Landscape and don’t want to resize everything between the two form-factors. Still for each direction there exist two entrys in this enum, and if you just do
(interfaceOrientation == UIDeviceOrientationPortrait)
you are just testing for one direction, not its variant turned around 180°. So please do the following instead:
UIInterfaceOrientationIsPortrait(interfaceOrientation);
//or
UIInterfaceOrientationIsLandscape(interfaceOrientation);
so finally your “shouldAutorotateToInterfaceOrientation” could look like this if you plan to show your App in Portrait Mode:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
This is not adding any extra work!
UIViews get rotated by 180° by UIKit, so allowing the user to turn the iPhone or iPad around can be achieved by changing one simple line per ViewController! There’s really no extra work in setting up all the frames because the formfactor stays the same, everything just turned upside down.
I understand this is completely obvious for all seasoned iOS Developers and was probably covered in depth on many times. Yet it still seems to happen in many Apps that I downloaded, so I thought I’d mention it.
June 20, 2011 at 11:39 pm
Thanks – was helpful for a noob!