我已经使用自定义detailCalloutAccessoryView实现了一个自定义标注气泡,里面有两个标签.
我知道如何用这一行改变detailCalloutAccessoryView的颜色.
view.detailCalloutAccessoryView?.backgroundColor = UIColor.red
但我无法弄清楚如何改变主气泡的背景颜色(现在它是透明的灰色/白色).使用view.detailCalloutAccessoryView?.backgroundColor = UIColor.red行我的calloutbubble看起来像这样:
但我希望我的自定义气泡看起来像这样:
这是我的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 }
我在Xcode 8 w / Swift 3中工作.
知道如何将标题的字体类型和默认黑色从黑色更改为另一种颜色也很有趣.
在详细视图中,我可以轻松更改xib文件中自定义标签的颜色,但不知道如何访问默认标题属性.
解决方法
链接:https://www.dropbox.com/s/o2howwqceq8rsgu/MapInformation.zip?dl=0
Environment : Xcode 8 and Swift3
我采用的方法是显示Popup(UIPresentationController)而不是callout.有关更多信息,请查看以下代码.
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 }
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) }
注意
If you want to change the popup color from red to other different
color then you can do only single line of coding by changing the color name.popupVC?.popoverPresentationController?.backgroundColor = UIColor.red
请看下面的截图.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。