Understanding iOS 6 Interface orientation change
Asked Answered
S

3

23

ADDED: I see that my question is viewed often without upvotes so I decided that you guys do not get what you search. Redirecting you to question that has really nice answer about How to handle orientation changes in iOS6

Specific demands to orientation changes: Restricted rotation

Upvotes are welcome :)


I've created a new project from Master Detail template and trying to start it with landscape orientation. As you know the

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

method is deprecated and we must use

- (NSUInteger)supportedInterfaceOrientations

and/or

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

Here's my code:

- (NSUInteger)supportedInterfaceOrientations {
    NSLog(@"supported called");
    return UIInterfaceOrientationMaskAll;//Which is actually a default value
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    NSLog(@" preferred called");//This method is never called. WHY?
    return UIInterfaceOrientationLandscapeRight;
}

As you can see I'm trying to return landscape orientation in preferred method but it is never called. p.s. documentation states:

Discussion The system calls this method when presenting the view controller full screen. You implement this method when your view controller supports two or more orientations but the content appears best in one of those orientations.

If your view controller implements this method, then when presented, its view is shown in the preferred orientation (although it can later be rotated to another supported rotation). If you do not implement this method, the system presents the view controller using the current orientation of the status bar.

So, the question is: Why the prefferredOrientation method is never get called? And how should we handle different orientations in different controllers?. Thanks! P.S don't mark the question as duplicate. I've investigated all similar questions and they do not have answer for mine.

Suite answered 8/10, 2012 at 9:15 Comment(2)
You can follow Apple's tutorial for same Supporting Multiple Interface Orientations Hope this will help you out.Behr
From your link: "When a view controller is presented over the root view controller, the system behavior changes in two ways. First, the presented view controller is used instead of the root view controller when determining whether an orientation is supported. Second, the presented view controller can also provide a preferred orientation. If the view controller is presented full screen, the user interface is presented in the preferred orientation." That's what I'm doing....or I just do not understand them..explain please in more details..Suite
S
34

About preferredInterfaceOrientationForPresentation

preferredInterfaceOrientationForPresentation is never called because this is not a "presented" view controller. There is no "presentation" involved here.

"Presented" and "presentation" are not some vague terms meaning "appears". These are precise, technical terms meaning that this view controller is brought into play with the call presentViewController:animated:completion:. In other words, this event arrives only if this is what we used to call a "modal" view controller.

Well, your view controller is not a modal view controller; it is not brought into play with presentViewController:animated:completion:. So there is no "presentation" involved, and therefore preferredInterfaceOrientationForPresentation is irrelevant here.

I'm being very explicit about this because I'm thinking that many folks will be confused or misled in the same way you were. So perhaps this note will help them.

Launch into Landscape

In iOS 6, the "Supported Interface Orientations" key in your Info.plist is taken much more seriously than previously. The solution to your overall problem of launching into a desired orientation is:

  1. Make sure that "Supported Interface Orientations" in your Info.plist lists all orientations your app will ever be allowed to assume.

  2. Make sure that the desired launch orientation is first within the "Supported Interface Orientations".

That's all there is to it. You should not in fact have to put any code into the root view controller to manage the initial orientation.

Shrunk answered 15/10, 2012 at 14:6 Comment(5)
@GeorgeAsda Yes it does. What do you think doesn't work? Actually it's even more important in iOS 8 than it ever was. See my question and answer here, from when iOS 8 first came out: #24464043Shrunk
@Shrunk in iOS8 and I assume to iOS9 there is NO way to force start in lets say landscape mode and then support ALL orientations. Regardless of what is the order in the .plist the app still launches however the user is holding the deviceBuchbinder
@GeorgeAsda "there is NO way to force start in lets say landscape mode and then support ALL orientations." Yes there is. That is exactly what my answer describes how to do. I have apps that do exactly this; I know that it works.Shrunk
@Shrunk well your instructions: "The solution to your overall problem of launching into a desired orientation is: Make sure that "Supported Interface Orientations" in your Info.plist lists all orientations your app will ever be allowed to assume. Make sure that the desired launch orientation is first within the "Supported Interface Orientations". That's all there is to it. You should not in fact have to put any code into the root view controller to manage the initial orientation." Does not workBuchbinder
@GeorgeAsda I just told you, it does work, and I have apps that do it. If you are doing something wrong, that's you doing it wrong. That doesn't make my answer wrong.Shrunk
S
8

If you would like launch modal view in Landscape Mode, just put this code in presented view controller

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    UIInterfaceOrientation orient = [[UIApplication sharedApplication] statusBarOrientation];
    if (UIInterfaceOrientationIsLandscape(orient)) {
        return orient;
    }

    return UIInterfaceOrientationLandscapeLeft;
}

Then, present this controller as usual

UIViewController *vc = [[UIViewController alloc] initWithNibName:nil bundle:nil];
[vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self.navigationController presentViewController:vc animated:YES completion:^{}];
Slowmoving answered 13/2, 2013 at 13:24 Comment(0)
Y
0

There is a very simple answer: You can only change or fix the interface orientation of a modal presented view controller. If you do so i.e. with a Present Modally segue in Interface builder (or the navigation controller method) you can define the allowed orientations with

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait; // for example else an addition of all allowed
}

This event doesn't fire up when you only push a view controller on the navigation Controller ... so : You don't have a BACK button and need a

[self dismissViewControllerAnimated:YES completion: ^(void) {

    }];

to close it.

Yasmeen answered 24/3, 2015 at 13:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.