在UITabBarController的标签栏前面呈现ViewController并隐藏此标签栏。

20
在我的项目中,我有一个UITabBarController。其中一个ViewControllers上有一个按钮。当我点击这个按钮时,会以模态方式呈现一个新的ViewController
问题是,当第二个ViewController呈现时,tabBarControllertabBar仍然可见。当我尝试在第一个ViewController的动作openFiltersList()中隐藏它时,使用以下方法: self.tabBarController?.tabBar.hidden = true 它会隐藏,但当我从第二个视图控制器返回并将此参数设置为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来设计界面。


你的模态视图嵌入了导航控制器吗? - TheSD
不,我的项目中没有任何导航控制器。 - vanelizarov
@TheSD 我也在做同样的事情,模态视图嵌入到导航控制器中。你能指导我吗? - Khalid Usman
2个回答

39

您应该从标签栏控制器中呈现新的视图控制器:

 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)

        })

    }

26
或者只需使用 modalPresentationStyle = .overFullScreen - Thibaud David

10

在Swift 5中,

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

设置popupController.modalPresentationStyle = .fullScreen如果您想在用户返回主屏幕时调用viewwillappear。 - Sourabh Sharma

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接