调整大小的图像,以适应MapView的leftCalloutAccessoryView
问题描述:
我想调整一个UIImage的大小,以适应leftCalloutAccessoryView
插槽中的地图视图的注释。我试图(很差)添加约束,但图像没有显示 - 没有它显示的限制,但它太大了。现在和他们在一起,它并没有出现,我在想,因为它已经被改变了视野。我尝试添加一个leftAnchor
和topAnchor
,但出现错误,因为我不完全确定我应该将其固定在哪个附件视图上?注释视图?反正这里的代码迄今:调整大小的图像,以适应MapView的leftCalloutAccessoryView
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let identifier = "Restaurants"
if annotation is Restaurants {
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView!.canShowCallout = true
let infoButton = UIButton(type: .detailDisclosure)
let imgView = UIImageView(image: UIImage(named: "TTT"))
// constraints
imgView.translatesAutoresizingMaskIntoConstraints = false
imgView.widthAnchor.constraint(equalToConstant: 20).isActive = true
imgView.heightAnchor.constraint(equalToConstant: 20).isActive = true
annotationView!.rightCalloutAccessoryView = infoButton
annotationView!.leftCalloutAccessoryView = imgView
} else {
annotationView!.annotation = annotation
}
return annotationView
}
return nil
}
难道我只是需要增加侧面和顶部锚,或者是那里要对这个完全不同的方式?
谢谢!
答
你可以尝试设置UIImageView
大小创建MKPinAnnotationView
,然后调用方面适合于这样的:
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: annotationView!.frame.height, height: annotationView!.frame.height))
imageView.image = UIImage(named: "TTT")
imageView.contentMode = .scaleAspectFit
annotationView!.leftCalloutAccessoryView = imageView
,像变魔术一样,谢谢! – KingTim