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

后端 未结 9 1688
终归单人心
终归单人心 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:55

    Having a similiar issue with my tab controller, however like the comments, I suggest changing the segue to a push or a show segue. This will allow that tab to remain in tact with the new view shown in place of the old view when switching to other tabs. If aesthetics is an issue you can make a custom navigation controller to customize the appearance of the new view.

    0 讨论(0)
  • 2020-12-04 19:56

    Try presenting the view controller in application window. I had a similar problem which was fixed by below code:

        let myNewVC = mainStoryBoard.instantiateViewController(withIdentifier: "MyNewVCId") as! MyNewVC
        let navController = UINavigationController(rootViewController: myNewVC)
        navController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext    
    
        let appDelegate = UIApplication.shared.delegate as? AppDelegate
        appDelegate?.window?.rootViewController?.present(navController, animated: true, completion: nil)
    

    Hope this helps you too.

    0 讨论(0)
  • 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.

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