Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone (iOS 5) forcing portrait from landscape and going back

I'm working on application for iOS 5 and I have one controller which needs to be pushed in portrait view. It's quite tricky to push controller in portrait mode from the landscape but finally I figured out how to do that. I show controller by presentModalViewController or by pushViewController and inside the controller I override preferredInterfaceOrientationForPresentation metod.

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

It works fine just it has one problem. Below is the scenario.

  1. User holds the device in landscape mode and current controller is correctly shown in landscape mode
  2. User make some action and the portrait only controller appears (as modal controller and in portrait mode)
  3. User close the modal controller
  4. Previous controller which works in landscape is now in portrait, but user still holds his device in landscape ---> This is the problem

I tried to fix it by calling [UIViewController attemptRotationToDeviceOrientation] but it doesn't help.

Thanks for any help in advance.

like image 531
Michal Avatar asked Nov 26 '25 07:11

Michal


2 Answers

Your saying that working with IOS 5, but preferredInterfaceOrientationForPresentation() method available from IOS 6 onwards only.

To solve your problem (in ios5) you needs to override the shouldAutorotateToInterfaceOrientation() method as like this...

in targeted view controller (View controller which you needed in portrait mode) as like this....

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}

UPDATED:

in the current view controller (View controller which you needed in landscape mode) as like this....

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
like image 195
Bhanu Prakash Avatar answered Nov 27 '25 20:11

Bhanu Prakash


I hope you get a better, working solution I did a nasty thing to get working. My problem it was I needed to support from 4.3 to 6.0. Some pushModal functions has changed radically, become deprecated and so on, also there are changes at rotations.

I wrote a RoatatingViewController and I have added to the root. Than I have added push modals and pushviewconcollers, as desired. When I need to pop, than I do a check: if the desired component on the stack is in desired orientation than is good, no changes. Otherwise: pop the landscape and push the portait instance and copy the landscape data to portait.

It isn't nice, it isn't memory efficient, but is working without crashing with all iOS versions. Good luck!


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!