UINavigationController: show embedded view controllers with different orientations after each transition?

后端 未结 1 1187
小蘑菇
小蘑菇 2020-12-30 16:37

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

Here are some of the posts considered:<

相关标签:
1条回答
  • 2020-12-30 17:07

    Step 1

    Subclass 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 }}
    }
    

    Step 2

    Use different UINavigationController for different orientations. Yes, pushing a new UINavigationController atop a previous UINavigationController will essentially be modal, but the transition looks nice.

    For added convenience, use User Defined Runtime Attributes to control the orientation of the LandscapeNavigationController.

    Step 3

    Add a pop method to handle Back buttons on the now modal UIViewController.

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

    In Action

    Notice how the Top and Bottom labels on view C are properly laid out.


    ↻ replay animation


    ► Find this solution on GitHub and additional details on Swift Recipes.

    0 讨论(0)
提交回复
热议问题