部分展示一个ViewController

时间:2018-10-02 15:32:11

标签: ios swift view uikit subview

我有一个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)
    }
} 

2 个答案:

答案 0 :(得分:0)

问题是我从按钮到ViewController创建了一个“显示”序列,这与将控制器添加到当前视图冲突,而是将其推入其中,并且MapViewController被释放了。

因此,我刚刚从sotryboard删除了segue并以编程方式添加了该孩子,它现在就像魅力一样工作。

这是一个愚蠢的错误,但这也许会帮助遇到相同问题的人:)

答案 1 :(得分:0)

尝试一下,视图从上到下显示,如下图所示: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
        }