cyclingIsBetter
cyclingIsBetter

Reputation: 17591

IOS: lock orientation in a UINavigationController

In my app I start the secondviewcontroller with a navigation controller in portrait orientation, and I finish in a fourthviewcontroller where I should rotate the iPhone to show an image. The problem now is: if in the target I set the orientation in this way: enter image description here

I can use this method to rotate my image:

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{

and it work fine, but I can't lock all view that belong at navigationcontroller, because all them rotate il portrait and landscape.

the second option is to set the target in this way: enter image description here

but this structure don't detect the orientation:

in view will appear:

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    [self willRotateToInterfaceOrientation:orientation duration:1];

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{

    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

- (BOOL)shouldAutorotate
{
        return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscape;
}

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {

        if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
        {

        }
        else
        {

        }
    }

    else{

        if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
        {

        }
        else
        {

        }

    }
}

what's the best solution? thanks

Upvotes: 2

Views: 1612

Answers (2)

ahmedalkaff
ahmedalkaff

Reputation: 313

Go with the first conifguration. And implement the method

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

for all view controllers. Allow landscape only for the wanted viewcontroller.

Upvotes: 0

Mick MacCallum
Mick MacCallum

Reputation: 130222

I can't help but feel that if I had read the docs it would have saved me some time, but for me the solution was to subclass UINavigaionController and add the following:

- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

And since there really isn't anything substantial going on here, there's no need to override init with coder or anything else. You can just reference this subclass everywhere you previously referenced your navigation controller, including Interface Builder.

Upvotes: 1

Related Questions