我有一张带有数十个注释的 map 。注释正确显示标题和副标题,我想在它们的右侧添加一个自定义“Go”按钮。 “前往”按钮将打开 Apple map ,并向用户指示他们在 map 上查看的注释。
我复制了下面我正在使用的代码。在当前的形式中,不会出现任何按钮。
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if !(annotation is MKUserLocation) {
let pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: String(annotation.hash))
let rightButton = UIButton(type: .custom)
rightButton.setImage(UIImage(named: "Go"), for: .normal)
rightButton.tag = annotation.hash
pinView.canShowCallout = true
pinView.rightCalloutAccessoryView = rightButton
return pinView
}
else {
return nil
}
}
如果我删除以下行:
rightButton.setImage(UIImage(named: "Go"), for: .normal)
并将其上面的更改为:
let rightButton = UIButton(type: .detailDisclosure)
按钮确实正确显示,这告诉我,我尝试为按钮使用自定义图像时出现问题。
对我缺少的东西有什么想法吗?
最佳答案
我能够通过将 UIButton 类型更改为以下内容来纠正它:
let rightButton = UIButton(type: .infoLight)
我的代码片段现在看起来是这样的:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if !(annotation is MKUserLocation) {
let pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: String(annotation.hash))
let rightButton = UIButton(type: .infoLight)
rightButton.setImage(UIImage(named: "Go"), for: .normal)
rightButton.tag = annotation.hash
pinView.canShowCallout = true
pinView.rightCalloutAccessoryView = rightButton
return pinView
}
else {
return nil
}
}
关于ios - map 注释不会显示 rightCalloutAccessoryView 的自定义 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41686875/