将视图控制器显示在 UITabBarController 的选项卡栏前面并隐藏此选项卡栏



在我的项目中,我有一个UITabBarController。在其中一个ViewControllers上有一个按钮。当我点击此按钮时,一个新的ViewController正在模态显示。

问题是,当第二个VC呈现时,tabBarControllertabBar仍然可见。当我试图隐藏它在第一个ViewController的行动openFiltersList()与这个方法:

self.tabBarController?.tabBar.hidden = true

它隐藏,但是当我试图取消隐藏它时,当我解散第二个VC时,将此参数设置为false不起作用,tabBar保持隐藏。下面是第一个和第二个的代码:

第一个(InvitesViewController, tabBarController的视图控制器之一):

func openFiltersList() {
        var filtersView : UIViewController = self.storyboard?.instantiateViewControllerWithIdentifier("filtersViewController") as! FiltersViewController
        filtersView.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext

        self.presentViewController(filtersView, animated: true) { () -> Void in
            UIView.animateWithDuration(0.3, animations: { () -> Void in
                filtersView.view.backgroundColor = UIColor(red: 0.0/255.0, green: 0.0/255.0, blue: 0.0/255.0, alpha: 0.5)
            })
        }
        self.tabBarController?.tabBar.hidden = true
    }

第二个(FiltersViewController,未嵌入任何地方):

@IBAction func dismiss(sender: AnyObject) { // close button action
        self.dismissViewControllerAnimated(true, completion: nil)
        var destinationVC : UIViewController = self.storyboard?.instantiateViewControllerWithIdentifier("invitesViewController") as! InvitesViewController
        destinationVC.tabBarController?.tabBar.hidden = false
    }

我使用storyboard作为界面

你应该从标签栏控制器显示新的viewController:

 self.tabBarController?.presentViewController(filtersView, animated: true) { () -> Void in
        UIView.animateWithDuration(0.3, animations: { () -> Void in
            filtersView.view.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5)
        })
    }

在Swift 5中

let popupController = ViewController()
popupController.modalPresentationStyle = .overFullScreen
self.present(popupController, animated: true, completion: nil)

最新更新