Ade*_*aer 5 mkmapview mkannotation mkannotationview swift swift3
在我的应用程序中,我有以下情况。
我已经实现了一个自定义标注气泡,其中包含自定义的详细信息CalloutAccessoryView,其中有两个标签。
我知道如何用这一行更改detailCalloutAccessoryView的颜色。
view.detailCalloutAccessoryView?.backgroundColor = UIColor.red
Run Code Online (Sandbox Code Playgroud)
但我不知道如何更改主气泡的背景颜色(现在是透明的灰色/白色)。使用 view.detailCalloutAccessoryView?.backgroundColor = UIColor.red 线条我的标注气泡如下所示:
但我希望我的自定义气泡看起来像这样:
这是我的 viewFor 注释方法:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
let identifier = "pin"
var view : MKAnnotationView
if let dequedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) {
dequedView.annotation = annotation
view = dequedView
} else {
view = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
view.canShowCallout = true
}
let pinImage = UIImage.init(named: "customPin")
DispatchQueue.main.async(execute: {
view.detailCalloutAccessoryView?.backgroundColor = UIColor.red
})
view.image = pinImage
configureDetailView(annotationView: view)
return view
}
Run Code Online (Sandbox Code Playgroud)
我正在使用 Xcode 8 w/Swift 3 工作。
了解如何将标题的字体类型和默认黑色从黑色更改为其他颜色也很有趣。在详细视图中,我可以轻松更改 xib 文件中自定义标签的颜色,但不知道如何访问默认标题属性。
我已根据您的要求创建了代码,请找到以下网址下载代码并查看。
链接: https: //www.dropbox.com/s/o2howwqceq8rsgu/MapInformation.zip ?dl=0
环境:Xcode 8 和 Swift3
突出显示我完成的代码。
UIPresentationController我采用了显示 Popup( ) 而不是标注的方法。欲了解更多信息,请查找以下代码。
A)我使用了UIButton在 MapView 上显示为注释并在用户单击它时显示弹出窗口。
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
let identifier = "pin"
var annotationView = self.mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as! AnnotationView?
if annotationView == nil {
annotationView = AnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView?.canShowCallout = false
}
else {
annotationView?.annotation = annotation
}
//Take the UIButton and implement the touchupinside action for showing the popup.
let pinImage = UIImage.init(named: "customPin")
annotationView?.frame = CGRect(x: 0, y: 0, width: (pinImage?.size.width)!, height: (pinImage?.size.width)!)
annotationView?.mapPin = UIButton(frame: (annotationView?.frame)!);
annotationView?.mapPin.addTarget(self, action: #selector(ViewController.showPopup(sender:)), for: .touchUpInside)
annotationView?.addSubview((annotationView?.mapPin)!)
annotationView?.mapPin.setImage(pinImage, for: .normal)
return annotationView
}
Run Code Online (Sandbox Code Playgroud)
B) 当用户单击注释时显示弹出窗口。
func showPopup(sender: UIButton!) {
let popupVC = self.storyboard?.instantiateViewController(withIdentifier: "Popup") as? Popup
popupVC?.preferredContentSize = CGSize(width: 250, height: 150)
popupVC?.modalPresentationStyle = UIModalPresentationStyle.popover
let rect = sender.superview?.convert(sender.frame, to: self.view)
popupVC?.popoverPresentationController?.delegate = self;
popupVC?.popoverPresentationController?.sourceView = self.view
popupVC?.popoverPresentationController?.sourceRect = rect!
popupVC?.popoverPresentationController?.backgroundColor = UIColor.red
self.present(popupVC!, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
笔记
如果您想将弹出颜色从红色更改为其他不同的颜色,那么您只需通过更改颜色名称即可执行一行编码。
popupVC?.popoverPresentationController?.backgroundColor = UIColor.red
请查看下面的屏幕截图。
| 归档时间: |
|
| 查看次数: |
4758 次 |
| 最近记录: |