UINavigationController: display built-in view controllers with different orientations after each transition?

This is a common question in StackOverflow, but none of the other solutions worked. Many were also written several years ago.

Here are some of the posts reviewed:

We have several view controllers built into the UINavigationController: A, B, C, D.

A, B use a portrait.

C, D use the landscape.

A is the root controller.

, B A. , B - . , C B, , , :

, , ; , .

, supportedInterfaceOrientations UINavigationController , .

, , .

UINavigationController ( , , , ):

extension UINavigationController {
    override open var shouldAutorotate: Bool {
        return true
    }

    override open var supportedInterfaceOrientations : UIInterfaceOrientationMask {
        return visibleViewController?.supportedInterfaceOrientations ?? UIInterfaceOrientationMask.landscapeRight
    }
}

:

override var shouldAutorotate: Bool {
    return true
}


override var preferredInterfaceOrientationForPresentation : UIInterfaceOrientation {
    return UIInterfaceOrientation.landscapeRight
}


override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.landscapeRight
}

, :

1) , UINavigationController .

2) VC (, C- > B , D- > C , B- > C , A- > B ).

UINavigationController ( ), . .

?

+4
1

1

UINavigationController.

class LandscapeNavigationController: UINavigationController {
    public var vertical: Bool = true

    override var shouldAutorotate: Bool {
        get { return true }}

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        get { return (vertical) ? .portrait : .landscapeLeft }}

    override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        get { return (vertical) ? .portrait : .landscapeLeft }}
}

2

UINavigationController . , UINavigationController UINavigationController , .

Storyboard with two navigation controllers

LandscapeNavigationController.

Runtime attributes

3

pop Back UIViewController.

@IBAction func doBack(_ sender: UIBarButtonItem) {
    if let navigationController = navigationController {
        navigationController.dismiss(animated: true, completion: {
        })
    }
}

, C.

Animation


GitHub .

+4

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


All Articles