简体   繁体   中英

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: 在此处输入图片说明

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: 在此处输入图片说明

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM