dimirc
dimirc

Reputation: 6605

Detect correct orientation on app launch

I have an universal app with a splitcontroller and a modal that is showing modally from the viewdidload of the detailcontroller (its a login screen)

When opening the ipad version, I want to be able to launch it on portrait or landscape depended on the initial orientation of the device. The problem is that its always launching as portrait (expected acording to documentation).

If I have the device as portrait and then turn if landscape, it works. But if I open the app directly landscape it doesnt.

BTW: I already set return true at (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation of that login viewController

update: If I perform the segue from viewDidAppear instead of viewDidLoad, the orientation of the modal works correctly but the SplitController is seen for a while before the modal, how can I avoid this?

- (void)viewDidAppear:(BOOL)animated
{
    [self.splitViewController performSegueWithIdentifier:@"toModal" sender:self.splitViewController];
}

Upvotes: 2

Views: 221

Answers (1)

starscream_disco_party
starscream_disco_party

Reputation: 2996

You can set the orientation of (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation conditionally. Something like this isn't pretty but it works:

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

if(orientation == 0) //Default orientation 
    //UI is in Default (Portrait) -- this is really a just a failsafe. 
else if(orientation == UIInterfaceOrientationPortrait)
    //Do something if the orientation is in Portrait
else if(orientation == UIInterfaceOrientationLandscapeLeft)
    // Do something if Left
else if(orientation == UIInterfaceOrientationLandscapeRight)
    //Do something if right

Otherwise this post seems pretty relative to what you're trying to do:

Launching application in landscape orientation for IPad


Proposal 2


You can create a class that has methods to find the orientation of the device (i.e. 'deviceInfo') using:

UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication]statusBarOrientation])

and

UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication]statusBarOrientation])

You can also store any stretch factors/dimensions/measurements you care to have. Then in the view in -(id)initWithFrame:(CGRect)frame you can call your methods to check orientation (i.e. deviceInfo.isLandscape?). Then set you view's autoresizingMask equal to UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth. Then, in -(void)layoutSubviews set the view.frame equal to whatever dimensions you want with:

CGRectMake(x-origin, y-origin, width, height);

Here's a reference for getting the current orientation:

http://www.ddeville.me/2011/01/getting-the-interface-orientation-in-ios/

Changing the dimensions of the view frame is relatively simple and can be found in Apple's documentation or some simple Googling.

Upvotes: 1

Related Questions