将一个 ViewController 部分呈现在另一个之上
Presenting one ViewController over another partially
我有一个 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)
}
}
问题是我创建了一个从按钮到 ViewController 的 segue "show",这与将控制器添加到当前视图发生冲突,它推入了它,而 MapViewController 已解除分配。
所以我刚刚从 sotryboard 中删除了 segue 并以编程方式添加了 child,它现在就像魅力一样。
这是一个愚蠢的错误,但也许这会对遇到同样问题的人有所帮助:)
试试这个,视图从顶部和背面显示,这是图像: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
}
我有一个 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)
}
}
问题是我创建了一个从按钮到 ViewController 的 segue "show",这与将控制器添加到当前视图发生冲突,它推入了它,而 MapViewController 已解除分配。
所以我刚刚从 sotryboard 中删除了 segue 并以编程方式添加了 child,它现在就像魅力一样。
这是一个愚蠢的错误,但也许这会对遇到同样问题的人有所帮助:)
试试这个,视图从顶部和背面显示,这是图像: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
}