Handling orientations in iPad


Introduction

I had the requirement to resize my controls for various orientations for an iPad Application. Since iPad User Experience Guidelines states that developer should aim to support all orientations. I came up with a framework which can provide me the necessary handling for various UIInterface orientation.

ViewController

I chosed to put this infrastructure into a BaseViewController which is an UIViewController and can be inherited by the Viewcontrollers that need to handle UIInterface orientations. Since in iOS applications, view controllers provide much of the logic needed to manage basic application behaviours. So why can’t we put the UIInterface orientation handling behavior here?

BaseViewController Design

//MSUIBaseViewController.h

@interface MSUIBaseViewController : UIViewController {
}

// Override to hanlde Interface Orientation
- (void)handleInterfaceRotationForOrientation:(UIInterfaceOrientation)interfaceOrientation;

@end

//MSUIBaseViewController

@implementation MSUIBaseViewController

#pragma mark -
#pragma mark ViewController Appearance Handling

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// Care should be taken since iPad has 2 more device orientations besides 4 UIInterface Orientation, FaceUp and FaceDown which are invalid in our case
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if(UIDeviceOrientationIsValidInterfaceOrientation(orientation)) {
[self handleInterfaceRotationForOrientation:orientation];
}
}

#pragma mark -
#pragma mark Screen Orientation Handling
- (void)handleInterfaceRotationForOrientation:(UIInterfaceOrientation)interfaceOrientation {
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
[self handleInterfaceRotationForOrientation:interfaceOrientation];
return YES;
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[self handleInterfaceRotationForOrientation:toInterfaceOrientation];
}

@end

Explanation

Just by inheriting from MSUIBaseViewController and overriding the handleInterfaceRotationForOrientation: and handling the resizing of the views under this method would suffice to cater the needs.

Points to be noted

  1. UIInterfaceOrientation has 4 orientations, 2 for portrait and 2 for landscape. Just overriding the shouldAutorotateToInterfaceOrientation method and handling here would be sufficient.
  2. In case of multiviews application where one can be top of others, if the orientation is changed on one view and made a transition to another view, the down view has no intimation about this.
  3. The above problem can be solved in two ways, adding notification to the device orientation change and another handling the orientation change on demand.
  4. Add Notification to device orientation change: This may demands the views which are not visible also to enact to the change.
  5. Acting according to the current device orientation: Every-time the view is visible, the framework calls viewWillAppear:animated method which would be the right place change the views resizing.
  6. Point to be pondered Device Orientation has 2 extra orientations to UIInterfaceOrienation called, UIDeviceOrientationFaceUp, UIDeviceOrientationFaceDown which are not intended orientations for us. Hence we have just filtered them here.
About these ads

About Mustafa Shaik

Yet another developer for mobile over Symbian, Windows Mobile & iPhone/iPad.

Posted on November 17, 2010, in Uncategorized. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: