ios - ViewController 作为 Swift 中 MapView 的叠加层(如 Google map 应用程序用户界面)

标签 ios swift uiviewcontroller segue uicontainerview

我正在使用 Swift 构建一个小型应用程序,我希望实现以下目标: - 带有多个注释的 map View - 单击其中一个注释会显示从底部移入的覆盖层中的一些详细信息,并填充大约。屏幕的一半 - 显示屏的上半部分仍然显示 map View 。单击 map 即可关闭详细信息

现在我已经阅读了很多有关不同可能性的内容。首先,我尝试使用 ContainerView 进行如下操作:

enter image description here

@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/

相关文章:

ios - 在 Parse Cloud Code 中为对象设置 PFFile

iphone - 我如何从 iphone 应用程序获取所有日历及其事件?

swift - SpriteKit 缩放

swift - SpriteKit 从 SKScene 回到菜单

ios - `UIViewController.present` 立即调用完成处理程序

ios - 打开群组 iMessage 的 url 方案是什么?

ios - HLS 流式传输到 IOS 设备

ios - 在 andom 索引处插入字符串无法快速工作

ios - Xcode 13.2 中为 iOS 13 宣布的 Swift Concurrency - 他们是如何实现这一目标的?

objective-c - 如何一次关闭 3 个模态视图 Controller ?