ios - 位置标注 View 上方 Pin Swift ios MKMapView

标签 ios swift mkmapview mkannotationview mkmapviewdelegate

我有一个带有自定义标注 View 的 MKAnnotation,它被添加到以下代码中:

func mapView(mapView: MKMapView,
             didSelectAnnotationView view: MKAnnotationView)
{

    if view.annotation is MKUserLocation
    {
        return
    }


    let customAnnotation = view.annotation as! MyAnnotation

    if (applicable) {
        let calloutView = CustomCallout()
        var r : MKMapRect = largeMapView.visibleMapRect
        let p : MKMapPoint = MKMapPointForCoordinate(customAnnotation.coordinate)
        r.origin.x = p.x - r.size.width * 0.5
        r.origin.y = p.y - r.size.height * 0.75

        // 3

        calloutView.translatesAutoresizingMaskIntoConstraints = false
        calloutView.imageView.translatesAutoresizingMaskIntoConstraints = false

        calloutView.clipsToBounds = true
        calloutView.imageView.clipsToBounds = true

        let mapPointCoordinate : CLLocationCoordinate2D = MKCoordinateForMapPoint(p)
        let pointAsCGPoint : CGPoint = self.largeMapView.convertCoordinate(mapPointCoordinate, toPointToView: self.largeMapView)
        calloutView.frame = CGRectMake(pointAsCGPoint.x, pointAsCGPoint.y, viewWidth! * 0.6, (viewHeight! - 110) * 0.6)
        calloutView.addSubview(calloutView.imageView)
        view.addSubview(calloutView)


        view.addConstraint(NSLayoutConstraint(item: calloutView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: (viewHeight! - 110) * 0.6))
        view.addConstraint(NSLayoutConstraint(item: calloutView, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: viewWidth! * 0.6))
        calloutView.addConstraint(NSLayoutConstraint(item: calloutView.imageView, attribute: .Top, relatedBy: .Equal, toItem: calloutView, attribute: .Top, multiplier: 1, constant: 0))
        calloutView.addConstraint(NSLayoutConstraint(item: calloutView.imageView, attribute: .Bottom, relatedBy: .Equal, toItem: calloutView, attribute: .Bottom, multiplier: 1, constant: 0))
        calloutView.addConstraint(NSLayoutConstraint(item: calloutView.imageView, attribute: .Leading, relatedBy: .Equal, toItem: calloutView, attribute: .Leading, multiplier: 1, constant: 0))
        calloutView.addConstraint(NSLayoutConstraint(item: calloutView.imageView, attribute: .Trailing, relatedBy: .Equal, toItem: calloutView, attribute: .Trailing, multiplier: 1, constant: 0))


        largeMapView.setVisibleMapRect(r, animated: true)

    }


}

当用户按下图钉时,我将 map 移动到水平居中,并以 25-75 的比例垂直于按下的图钉。现在我试图将 CustomCallOut 居中放置在图钉上方的正中央,但无论我做什么,它似乎都在屏幕上不规则地定位自己。

最佳答案

使用自动布局时,您无需调整frame 值,也无需关心 map 坐标。只需添加与注释 View 相关的约束。

例如,这会在 MKPinAnnotationView 正上方添加一个 60x30 的空白灰色标注 View :

func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
    let calloutView = UIView()
    calloutView.translatesAutoresizingMaskIntoConstraints = false
    calloutView.backgroundColor = UIColor.lightGrayColor()
    view.addSubview(calloutView)

    NSLayoutConstraint.activateConstraints([
        calloutView.bottomAnchor.constraintEqualToAnchor(view.topAnchor, constant: 0),
        calloutView.widthAnchor.constraintEqualToConstant(60),
        calloutView.heightAnchor.constraintEqualToConstant(30),
        calloutView.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor, constant: view.calloutOffset.x)
    ])
}

因此,将标注的底部 anchor 设置为相对于 MKAnnotationView,并将 centerX 设置为偏移 calloutOffset

显然,您可以将标注的内容和大小设置为任何您想要的,但我希望这说明了使自定义标注 View 在注释 View 上居中的方法。

关于ios - 位置标注 View 上方 Pin Swift ios MKMapView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38819538/

相关文章:

objective-c - iOS:简单的动画

iphone - MKMapView 上的多个 MKOverlay 会导致内存警告

ios - MKMapView注解第二次不可选

ios - 快速向 callOut View 添加标题

ios - 如何将 SVGKit 包含到我的 Rubymotion 项目中?

ios - CCJumpTo 和 CCJumpBy 高度参数?

ios - 如何使用 Swift 的 prepareForSegue 函数指定要准备的 View Controller ?

swift - Swift 中的可哈希枚举

ios - 应用程序在切换到前置摄像头时崩溃

Swift Realm 通过列表属性元素查询