我有一个带有自定义MKAnnotation视图的CallOut,它添加在以下代码中:
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)
}
}当用户按下引脚时,我将地图移动到水平中心,垂直于按下引脚的25-75比率。现在我试着把CustomCallOut的中心放在引脚上方,但是不管我做什么,它似乎在屏幕上不稳定地定位自己。
发布于 2016-08-08 02:00:57
在使用自动布局时,您不必调整frame值,也不需要考虑地图坐标。只需添加相对于注释视图的约束。
例如,这会添加一个空白的灰色标注视图,该视图位于MKPinAnnotationView的正上方60x30。
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)
])
}因此,将标注的底部锚设置为相对于MKAnnotationView,并将centerX设置为由calloutOffset偏移。
显然,您可以将您的标注的内容和大小设置为您想要的任何内容,但我希望这说明了如何使自定义callout视图以注释视图为中心。
https://stackoverflow.com/questions/38819538
复制相似问题