Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle different orientations in iOS

ADDED: You can access this project on github ios6rotations


Sorry guys for asking the question about screen rotation in iOS 6 but this is really a pain in the ass..and I still can't understand it completely - for some reason it behaves differently under certain circumstances.

I have the following simple hierarchy of views in my test app:

What I'm trying to achieve is - to keep blue controller in landscape only and red one is only in portrait.

I have a subclass of UINavigationController with such code inside:

@implementation CustomNavController

- (BOOL)shouldAutorotate
{
    return [[self.viewControllers lastObject] shouldAutorotate];
}

- (NSUInteger)supportedInterfaceOrientations
{
    return  [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}

@end

In my blue controller I implemented this:

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

And in red controller this:

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

Now I have the following behavior:

  1. App started in landscape (OK)
  2. When I press the button my red controller pushed in landscape too (this is not ok because it must be shown in Portrait)
  3. It successfully rotates to portrait but not backward to landscape
  4. If I leave the red controller in Portrait mode my blue controller (which is restricted to landscape) shows in Portrait mode.

P.S. All my rotation methods(posted above) are getting called normally.(by the way why do these methods getting called so many times per screen transition - 5-6 times)

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation does not getting called with pushing

All(except portraitUpsideDown) orientations are included in plist.

The question is - how to force rotation to supported orientation in each controller?

I suggest you to post here (as answers) any 100% working code to handle rotations in ios6 (for example if you have some for iPad with SplitController) - I'll keep this question in favorites to have all in one place when I need to handle some specific situations. Cheers!

ADDED: Please do not post this as answer from landscape to portrait I hope that there' s more elegant way to do it.

like image 245
Stas Avatar asked Apr 11 '13 11:04

Stas


People also ask

How do I force an app to landscape in iOS?

On an iPhone without a Home button, swipe down from the top-right corner of the screen instead. Here, tap on the rotation lock icon (which looks like a lock with a circular arrow) to turn it on or off.

How do I change orientation in iOS?

Swipe down from the top-right corner of your screen to open Control Center. Tap the Portrait Orientation Lock button to make sure that it's off. Turn your iPhone sideways.

Can you change the orientation of an app?

To change the orientation of individual apps, simply tap on them and select a new orientation mode from the list of available choices. The first three options define the new orientation for the app. They can be landscape, portrait or auto, which basically sets the orientation automatically.


3 Answers

Using -[UIDevice setOrientation:] is a private API, and will get your application rejected. See this question.

What you ask is not possible using public API and is also not recommended from HIG standpoint. What is supported and you should implement, is modal presentation of the different view controllers with different supported interface orientation. This is why the default implementation of UINavigationController is to always rotate; it assumes all view controllers have the same supported interface orientations.

Take for example video playback on iPhone. Open the video apps (that comes with iOS). The root view controller only supports portrait orientation. However, start a video, and a modal view controller pops up which only supports landscape interface orientations. This seems exactly the behavior you wish to achieve.

This is why preferredInterfaceOrientationForPresentation is not called. preferredInterfaceOrientationForPresentation only gets called when using presentViewController:animated:.

A small gotcha, if you require a navigation bar in each stage of your scene, you will need to enclose each modal view controller with a navigation controller. You can then pass the required data in prepareForSegue: by accessing topViewController of the navigation controller object in the segue.


Here is an example project which behaves correctly according to your requirements (or at least will give you ideas how to implement):

http://www.mediafire.com/?zw3qesn8w4v66hy

like image 119
Léo Natan Avatar answered Oct 02 '22 20:10

Léo Natan


My two cents worth.

You can present an empty transparent modal view quickly then dismiss it, maybe on ViewDidLoad: or viewWillAppear: on your ViewController and ViewControllerSecond class as a quick workaround.

Also, in storyboard, you can set ViewController class orientation to landscape visually.

like image 24
dklt Avatar answered Oct 02 '22 20:10

dklt


use this line for programmatically change orientation... work 100%

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

and also when you add this line at that time one warning appear and for remove this warning just add bellow code on you implementation file.. at the top.

@interface UIDevice (MyPrivateNameThatAppleWouldNeverUseGoesHere)
- (void) setOrientation:(UIInterfaceOrientation)orientation;
@end

and after that in bellow method just write this code if required..

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return your supported orientations

 if (currentMainView==blueOne) {
        return toInterfaceOrientation== UIInterfaceOrientationPortrait;
    }
}
like image 23
Omarj Avatar answered Oct 02 '22 20:10

Omarj