我有一个MapViewController,基本上是一个MKMapView,并提供了带有自定义注释的地图。

现在,我试图从底部显示另一个ViewController,其中包含用户可以在旅途中使用的其他过滤器。

但是我遇到了一个问题,当我将新的FilterMenuViewController作为MapViewController的子级出现时,MapViewController消失了。

看起来是这样的:

Initial state

Button to present new controller tapped

New controller presented but the MapViewController disappearing

处理交互的代码如下:

MapViewController变量声明:

var filterMenuVC = FilterMenuViewController()
var isFilterMenuOpened = false


MapViewController viewDidLoad()

    filterMenuVC = storyboard?.instantiateViewController(withIdentifier: "FilterMenuViewController") as! FilterMenuViewController


MapViewController showFilterMenu按钮操作:

@IBAction func showFilterMenu(_ sender: UIButton) {
    // Presents the filter menu
    if isFilterMenuOpened == true {
        isFilterMenuOpened = false
        filterMenuVC.willMove(toParentViewController: nil)
        filterMenuVC.view.removeFromSuperview()
        filterMenuVC.removeFromParentViewController()
    } else if isFilterMenuOpened == false {
        isFilterMenuOpened = true
        self.addChildViewController(filterMenuVC)
        self.view.addSubview(filterMenuVC.view)
        filterMenuVC.didMove(toParentViewController: self)
    }
}

最佳答案

试试看,视图从上到下显示,如下图所示:https://imgur.com/a/gzIGjEa

if isFilterMenuOpened{
            if let searching = self.childViewControllers.first as? ViewController{
                UIView.animate(withDuration: 0.7, animations: {
                    searching.view.frame = CGRect.init(x: 0,
                                                       y: self.view.frame.origin.y-308,
                                                       width: self.view.frame.size.width,
                                                       height: 308)
                }, completion: {(boos) in
                    searching.removeFromParentViewController()
                    searching.dismiss(animated: true, completion: nil)
                    self.isFilterMenuOpened = false
                })
            }
        }else{
            let search = self.storyboard?.instantiateViewController(withIdentifier: "searchView") as! ViewController
            search.view.frame = CGRect.init(x: 0, y: self.view.frame.origin.y-308, width: self.view.frame.size.width, height: 308) //The View controller height same size of your view in storyboard
            search.delegateSearch = self
            self.addChildViewController(search)
            UIView.animate(withDuration: 0.5, animations: {
                search.view.frame = CGRect.init(x: 0, y: self.view.frame.origin.y, width: self.view.frame.size.width, height: 308)
            })
            self.view.addSubview(search.view)
            self.didMove(toParentViewController: search)
            isFilterMenuOpened = true
        }

关于ios - 部分展示一个ViewController,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52611662/

10-08 20:57