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

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

    iOS 10+ & Swift 3+

    I've very nice solution of this problem. Use, over full screen modal presentation style for a view controller, is being presented.

    let storyboard = UIStoryboard(name: "Main", bundle: nil)  // Replace “Main” with your storyboard name
    
    if let viewController = storyboard?.instantiateViewController(withIdentifier: “viewController Identifier”) as? ViewController {
                viewController.modalPresentationStyle = .overFullScreen
                self.present(viewController, animated: false, completion: { 
                })
            }
    

    Over full screen will present your view controller over your tabbar (controller) by covering it. So, end-user can't switch tabbar (unless you perform operation programatically) tabbar items. User must close this view controller to switch tabbar.

    If you are using segue, to present view controller then select 'Over Full Screen' from modal presentation style in Attribute inspection

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

    Try to set your presentation style as 'Over Full Screen' instead of 'Over Current Context' in storyboard.

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

    i had this problem : when i present my ModalController it takes transparent background and when i changed the tab to next one in tabBarController and getBack to previous one the transparent background was gone and and there is a bad black background after research i found the point the point is this:

    self.definesPresentationContext = true

    the self is not modal controller self is that presenting controller for modalController and another point is .overCurrentContext like this

    self.definesPresentationContext = true
            modalController.modalPresentationStyle = .overCurrentContext
            self.present(modalController, animated: true, completion: nil)
    
    0 讨论(0)
  • 2020-12-04 19:48

    I had the same issue and was able to solve it by setting self.definesPresentationContext = YES; on the presenting view controller before presenting the modal VC. You can also set this in a storyboard, the checkbox is called "Defines Context" in Interface Builder.

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

    I reproduced your problem and found a solution. It does not involve changing the segue method or changing some attributes in the storyboard.

    Suggestion:

    But before going to the solution I would like to add that the purpose of modally presented viewcontrollers is to distrupt the actual flow of the app and present some extra contextual information or some actionable contents for the presenting vc. Simply put it is very logical and actually recommended to cover the tab bar when presenting a view controller modally. There are lots of good examples of it in the app store.

    Having said that here is a solution I propose.

    Solution:

    I believe the problem lies in the way UITabBarController handles its view hierarchy.

    What I did was explicitly dismiss the modally presented view controller before the tab was changed. This made the presenting view controller persist in the view hierarchy of the UITabBarViewController before the tab bar switches to new tab.

    In the "viewWillDisappear" method of your modally presented ViewController add this.

    - (void)viewWillDisappear:(BOOL)animated {
        [self dismissViewControllerAnimated:true completion:^{
        [super viewWillDisappear:animated];
      }];
    }
    
    0 讨论(0)
  • 2020-12-04 19:52

    I also needed a Navigation Controller before the calling ViewController!

    And I used the described code before:

    let newVC = self.storyboard?.instantiateViewController(withIdentifier: "newViewController")
            self.definesPresentationContext = true
            self.providesPresentationContextTransitionStyle = true
            newVC?.modalPresentationStyle = .overCurrentContext //do NOT use .currentContext --> it will produce a black screen by switching over tabbar and close VC
            self.present(newVC!, animated: false)
    
    0 讨论(0)
提交回复
热议问题