简体   繁体   中英

UIViewcontroller is not presented in correct orientation

I am presenting a view controller from another view controller using presentViewController .

The presenting view controller (The "SourceViewController") creates the new view controller and assigns it to a navigation controller before presentation (because the "NextViewController" wants a navigation bar and navigation controller).

// from the source view controller
@implementation SourceViewController

-(void)showNextViewController
{
NextViewController *viewController = [[NextViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];

[self presentViewController:viewController animated:YES];
}
@end


@implementation NextViewController
// in NextViewController

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

@end

But when I present the view controller when the originating view controller is in landscape the "NextViewController" isn't presented in portrait but rather in landscape like the source view controller.

I've tried many combinations of rotation methods but haven't been able to get it to present in the correct orientation.

I assume that it is possible because many apple components like UIImagePickerController are always presented in portrait , so how do I force its orientation?

Thanks

EDIT :

I've created a UINavigationController sub class:

PortraitNavigationController : UINavigationController

@implementation

-(BOOL)shouldAutorotate
{
    return NO;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

@end

and then when presenting the NextViewController

PortraitNavigationController *nav = [PortraitNavigationController initWithRootViewController:nextViewController];

[self presentViewController:nav animated:YES];

and now NextViewController is indeed in portrait - but when I rotate the device to use this view controller and eventually dismiss it - the underlying source view controller looks all messed up.

The underlying view controller is a custom container view controller which is embedded in a UINavigationController

The containers it uses to display the child view controllers are not in their correct places

I don't want the container view controller to rotate at all as soon as the NextViewController is displayed and dismissed.

When you rotate your device the presented view controller is asked about the rotations and orientations it supports - In your case it's a UINavigationController and not an instance of NextViewController. One way to fix this is to subclass UINavigationController and override the rotation methods and forward the calls onto it's root view.

As a side not UIImagePickerController is a subclass of UINavigationController. It might make more sense to make NexViewController a subclass of UINavigationController and then inside that subclass initialize it with the correct root view controller.

Another option is to just alloc and init a UINavigationBar inside of NextViewController and add it as a subview if you don't need to use the navigation controller for anything. In this case autolayout comes in handy because you can pin it to the top, left, and right and let it figure out the correct size and location for it.

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