Is it possible to have different orientations for viewing controllers inside a UINavigationController?

I want all view controllers to support only portrait mode, except that one view controller allows you to call it "LandscapeSupportViewController", which should also support landscape mode.

The problem is that I am in LandscapeSupportViewController in landscape mode, and then I click on a new view controller that only supports portrait mode, the pushed controller will also be in landscape mode! How can I make him be a portrait?

I saw several applications that do this, for example, the iPhone application for iPhone, the Messages tab is only portrait โ†’ then if you press to enter the message itself, you will get a view controller that supports landscape as well , as it does to enable landscape mode, when the user is talking โ†’, then if you click to view the faces profile, the new controller will be pressed, but a portrait will be executed! the same thing will happen if you return, you will be forced to return to the portrait, even if you came from a landscape ...

thank

+2
source share
4 answers

, , , : ( App Store ) , ( , , ). Skype , , , "", "" .

, , , Apple.

+1

, .

[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationPortrait animated:NO];

, , if. .

!

+1

, viewController, . LandscapeViewController

[appdel.navigationController.view removeFromSuperview];// This navcontroller used with rootviewcontroller
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
[ [UIApplication sharedApplication].self.delegate.window addSubview:appdel.navigationController.view];
self.navigationController.navigationBar.hidden=NO;
+1

. UINavigationController, . . :

@interface UINavigationController (MyViewOrientations)
@end

@implemetation UINavigationController (MyViewOrientations)

- (BOOL)supportLandscapeModeForViewController:(UIViewController *)controller {
    return [controller isKindOfClass:[LandscapeSupportViewController class]]
}

- (NSUInteger)supportedInterfaceOrientation {
    UIViewController *controller = [self visibleViewController];
    NSUInteger orientationMasks = UIInterfaceOrientationMaskPortrait
    if([self supportLandscapeModeForViewController:controller]) {
        orientationMasks |= UIInterfaceOrientationMaskLandscapeLeft;
        orientationMasks |= UIInterfaceOrientationMaskLandscapeRight;
    }
    return orientationMasks;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    UIViewController *controller = [self visibleViewController];
    if([self supportLandscapeModeForViewController:controller]) {
        return UIInterfaceOrientationLandscapeLeft; // Your call
    }
    else {
        return UIInterfaceOrientationPortrait;
    }
}

- (BOOL)shouldAutorotate {
    UIViewController *controller = [self visibleViewController];
    return [self supportLandscapeModeForViewController:controller];
}
@end

If the situation is more complicated, different views support different orientations. You can override "supportedInterfaceOrientation", "preferredInterfaceOrientationForPresentation", "shouldAutorotate" in your view controllers and delegate calls from UINavigationController category code using "visibleViewController".

+1
source

Source: https://habr.com/ru/post/1665553/


All Articles