Nic Hubbard
Nic Hubbard

Reputation: 42139

modalViewController presented from UISplitViewController comes up as the wrong orientation

I have a UISplitViewController that is set at the rootView of my application. When viewDidLoad is called in my left view controller I do a check, then present a modal view controller using the following:

SiteConfiguration *config = [[SiteConfiguration alloc] initWithStyle:UITableViewStyleGrouped];
config.firstLoad = YES;
UINavigationController *configNav = [[UINavigationController alloc] initWithRootViewController:config];
if ([Utility isIpad]) {
    configNav.modalPresentationStyle = UIModalPresentationFormSheet;
    configNav.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [[AppDelegate instance].splitViewController presentModalViewController:configNav animated:YES];
} else {
    [self presentModalViewController:configNav animated:YES];
}

If the iPad is in landscape mode while the app loads, the modalView is shown with an incorrect orientation:

enter image description here

I can rotate the iPad to fix this, but WHY does it load up wrong? I have shouldAutorotateToInterfaceOrientation: returning YES in my SiteConfiguration viewController. What could be causing this?

Upvotes: 2

Views: 1006

Answers (2)

Pedro Cattori
Pedro Cattori

Reputation: 2805

Be careful of where you choose to present your modal controller. I've had experience with some custom modal controllers and setting the orientation of the modal controller (and its shadows!) in

 - (void)viewDidLoad:(BOOL)animated 

didn't always behave as expected.

Put your code (presentModalViewController:configNav animated:YES) in

 - (void)viewDidAppear:(BOOL)animated 

instead. (Do this as well with any code that sets a subviews frame or does any manipulation of layers, e.g. the shadow layer and shadow properties).

As far as I can tell, the rotation may not be apparent to subviews of the rotated view until after

 - (void)viewDidLoad:(BOOL)animated 
due to threading issues (one thread may start drawing your subview or modal controller's view before rotation is passed down to the subviews (and modal controllers) by the main thread). Someone with more experience with threads than myself might be able to shed more light on this.

Upvotes: 3

Richard
Richard

Reputation: 3296

shouldAutorotateToInterfaceOrientation: doesn't actually rotate the interface, the app does that upon receiving a UIDeviceOrientationDidChangeNotification notification.

Try adding a check for the device orientation in the -(void) viewDidAppear:(BOOL)animated method.

To force an interface rotation, use the following piece of code.

UIDeviceOrientation toInterfaceOrientation = [[UIDevice currentDevice] orientation];
[UIApplication sharedApplication].statusBarOrientation = toInterfaceOrientation;

Upvotes: 0

Related Questions