The iOS project (Swift4.2) is in the form, and the interface defaults to vertical screen. How to set some specific interface horizontal and vertical screen to switch or force horizontal screen?

problem description

App uses TabbarController- > NavigationController- > ViewController architecture. You want the overall interface to default to vertical screen, and then some specific interfaces can support horizontal and vertical screen switching or forced horizontal screen.

the environmental background of the problems and what methods you have tried

related codes

in custom UINavigationController

override var shouldAutorotate: Bool {
        return self.topViewController?.shouldAutorotate ?? false
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return self.topViewController?.supportedInterfaceOrientations ?? .portrait
    }

    override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        return self.topViewController?.preferredInterfaceOrientationForPresentation ?? .portrait
    }

Custom UITabBarController:

override var shouldAutorotate: Bool {
        return self.selectedViewController?.shouldAutorotate ?? false
        
    }
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return self.selectedViewController?.supportedInterfaceOrientations ?? .portrait
    }
    
    override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        return self.selectedViewController?.preferredInterfaceOrientationForPresentation ?? .portrait
    }

then what I do now is, in the VC that only wants the vertical screen:

override var shouldAutorotate: Bool {
        return false
    }
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }
    
    override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        return .portrait
    }

in VC where you only want to landscape the screen:

override var shouldAutorotate: Bool {
        return true
    }
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return [.landscapeRight, .landscapeLeft]
    }
    
    override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        return .landscapeRight
    }

in VC with switchable horizontal and vertical screens:

override var shouldAutorotate: Bool {
        return true
    }
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .allButUpsideDown
    }
    
    override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        return .landscapeLeft
    }

the horizontal and vertical screens are checked in the info.plist of the project. In this way, there are several bug, one of which is from the vertical screen interface push to the horizontal and vertical screen switchable interface, and then sideslip back when the interface is horizontal, and then on the first interface, when the push, slips back again, the animation of the side slip returns disappears, and the transition effect is very abrupt.

is there a perfect solution? Thank you again!

Nov.02,2021
Menu