ViewController作为Swift中的MapView覆盖(如Google Maps应用程序用户界面)

我正在用Swift创build一个小应用程序,我想要实现以下function: – 带有几个注释的MapView – 点击其中一个注释显示一个覆盖层中的一些细节,从底部移入, 屏幕的一半 – 显示屏的上半部分仍然显示MapView。 点击地图上的closures细节

现在我读了很多关于不同的可能性。 首先,我试着用这样的东西来使用ContainerView:

在这里输入图像说明

@IBOutlet weak var detailsContainerYPosition: NSLayoutConstraint! func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!) { UIView.animateWithDuration(0.3, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut, animations: { self. detailsContainerYPosition.constant = 0 self.view.layoutIfNeeded() }, completion: nil) } 

问题是,viewDidLoad()方法只被触发一次,我想用它来build立详细信息页面后,将一些数据传递到控制器类。

所以接下来我玩了一个自定义的赛车,并与此相提并论:

 class DetailsSegue: UIStoryboardSegue { override func perform() { // Assign the source and destination views to local variables. var mapView = self.sourceViewController.view as UIView! var detailsView = self.destinationViewController.view as UIView! // Get the screen width and height. let screenWidth = UIScreen.mainScreen().bounds.size.width let screenHeight = UIScreen.mainScreen().bounds.size.height // Specify the initial position of the destination view. detailsView.frame = CGRectMake(0.0, screenHeight, screenWidth, 140) // Access the app's key window and insert the destination view above the current (source) one. let window = UIApplication.sharedApplication().keyWindow window?.insertSubview(detailsView, aboveSubview: mapView) // Animate the transition. UIView.animateWithDuration(0.4, animations: { () -> Void in detailsView.frame = CGRectOffset(detailsView.frame, 0.0, -140) }, completion: nil) } } 

这似乎是更好的解决scheme,因为我可以将一些数据发送到prepareForSegue()函数内的新控制器,并且viewDidLoad()方法在每次启动这个segue时被触发,但现在我正在挣扎: – 每次单击注释并调用segue,插入一个新的View。 但是,如果一个细节视图已经可见,我想只是更新这一个 – 我无法find任何方式来解开这个segue时,从MapView调用didDeselectAnnotationView。

但也许这里的任何人有更好的方式来实现这样的用户界面。

我终于设法得到我想要的手动添加ViewController和一个这样的视图:

 let detailsWidth: CGFloat = view.bounds.width let detailsHeight: CGFloat = 150 let detailsViewFrame: CGRect = CGRectMake(0, view.bounds.height, detailsWidth, detailsHeight) detailsController = storyboard?.instantiateViewControllerWithIdentifier("details") as! DetailsViewController detailsController.descriptionText = "I'm a text that was passed from the MainViewController" self.addChildViewController(detailsController) detailsController.view.frame = detailsViewFrame view.addSubview(detailsController.view) detailsController.didMoveToParentViewController(self) 

而当解散细节时,我删除了这样的控制器:

 self.detailsController.removeFromParentViewController() 

我创build了一个小样本来演示我的解决scheme: https : //github.com/flavordaaave/iOS-Container-View-Overlay