Black screen after presenting modal view controller in current context from UITabBarController

后端 未结 9 1692
终归单人心
终归单人心 2020-12-04 18:53

My root view controller is a UITabBarController. I\'m trying to present a modal view controller over one of the tab bar controller\'s view controllers, but still allow the

9条回答
  •  时光取名叫无心
    2020-12-04 19:56

    I have same problem in currently live swift project. I have did workaround on that.

    Then finally i have used NSNotificationCenter and dismiss that view controller when tab is changed to solve this problem.

    I have referenced tabbar controller in AppDelegate and set delegate there.

    let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    
    let tabbarcontroller = storyboard.instantiateViewControllerWithIdentifier("MyTabBarController") as! UITabBarController
    
    tabbarcontroller.delegate = self
    

    And it's delegate as

    //MARK: - Tabbar controller delegate
    
    extension AppDelegate : UITabBarControllerDelegate {
    
        func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
    
            NSNotificationCenter.defaultCenter().postNotificationName("TabBarTabChanged", object: nil)
        }
    }
    

    Then I have add observer in my presented view controller as

    override func viewDidLoad() {
            super.viewDidLoad()
    
            // Do any additional setup after loading the view.
            NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(PreviewPlaceVC.BackClickAction(_:)), name: "TabBarTabChanged", object: nil)
    }
    
    // MARK: - Button Click Actions
    
    @IBAction func BackClickAction(sender: AnyObject) {
         self.dismissViewControllerAnimated(true, completion: nil)
    }
    

    And it's work fine for me. Not a proper solution I think to dismiss view controller on tab change event, but it is ok to dismiss rather than black screen which also breaks navigation that time.

提交回复
热议问题