我正在使用 Swift 构建一个小型应用程序,我希望实现以下目标: - 带有多个注释的 map View - 单击其中一个注释会显示从底部移入的覆盖层中的一些详细信息,并填充大约。屏幕的一半 - 显示屏的上半部分仍然显示 map View 。单击 map 即可关闭详细信息
现在我已经阅读了很多有关不同可能性的内容。首先,我尝试使用 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() 方法仅被触发一次,我想在将一些数据传递给 Controller 类后使用它来构建详细信息页面。
接下来我尝试了一个自定义的转场并想出了这个:
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)
}
}
这似乎是更好的解决方案,因为我可以在prepareForSegue()函数中向新 Controller 发送一些数据,并且每次启动此segue时都会触发viewDidLoad()方法,但现在我正在挣扎: - 每次单击注释并调用 segue 时,都会插入一个新 View 。但如果一个详细 View 已经可见,我只想更新这个 - 当从 MapView 调用 didDeselectAnnotationView 时,我找不到任何方法来展开此 segue。
但也许这里的任何人都有更好的方法来实现这样的 UI。
最佳答案
我终于设法通过手动添加 ViewController 和这样的 View 来获得我想要的东西:
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)
当忽略详细信息时,我像这样删除 Controller :
self.detailsController.removeFromParentViewController()
我创建了一个小样本来演示我的解决方案: https://github.com/flavordaaave/iOS-Container-View-Overlay
关于ios - ViewController 作为 Swift 中 MapView 的叠加层(如 Google map 应用程序用户界面),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29633740/