如何使用 detailCalloutAccessoryView 更改标注气泡的默认背景颜色
How to change default background color of callout bubble with detailCalloutAccessoryView
在我的应用程序中,我有以下情况。
我已经实现了一个带有自定义 detailCalloutAccessoryView 的自定义标注气泡,里面有两个标签。
我知道如何用这一行改变 detailCalloutAccessoryView 的颜色。
view.detailCalloutAccessoryView?.backgroundColor = UIColor.red
但我不知道如何更改主气泡的背景颜色(现在它是透明的 grey/white)。使用 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 文件中自定义标签的颜色,但不知道如何访问默认标题属性。
UIViewCallout
是私有 class。如果您想要自定义标注视图:
禁用标准标注view.canShowCallout = false
使用您自定义的 UIView
实现 MKMapViewDelegate
方法用于标注:
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
let redCalloutView = RedCalloutView(view.annotation)
view.addSubview(redCalloutView)
}
func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
view.subviews.forEach {
if [=10=] is RedCalloutView {
[=10=].removeFromSuperview()
}
}
}
我已经根据您的要求创建了代码,请在下面找到 url 下载代码并查看它。
Link : https://www.dropbox.com/s/o2howwqceq8rsgu/MapInformation.zip?dl=0
Environment : Xcode 8 and Swift3
突出显示我完成的代码。
我采用了显示 Popup(UIPresentationController
) 而不是标注的方法。欲了解更多信息,请找到以下代码。
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
}
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)
}
注
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
请看下面的截图。
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
for v in view.subviews{
if v.subviews.count > 0{
v.subviews[0].backgroundColor = UIColor.red
}
}
}
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
for v in view.subviews{
if v.subviews.count > 0 {
let colloutView = v.subviews[0]
colloutView.backgroundColor = UIColor(red: 0.0/255.0, green: 0.0/255.0, blue: 0.0/255.0, alpha: 0.8)
if colloutView.subviews.count > 0 {
if colloutView.subviews[0].subviews.count > 0{
colloutView.subviews[0].subviews.forEach { (view) in
if let label = view as? UILabel{
label.textColor = UIColor.white
}
}
}
}
}
}
}
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
//design your custom view and set it to detailCalloutAccessoryView
view.detailCalloutAccessoryView = yourDetailView
detail.superview?.superview?.backgroundColor = yourColor
// This view is type of MKSmallCalloutView
}
在我的应用程序中,我有以下情况。
我已经实现了一个带有自定义 detailCalloutAccessoryView 的自定义标注气泡,里面有两个标签。
我知道如何用这一行改变 detailCalloutAccessoryView 的颜色。
view.detailCalloutAccessoryView?.backgroundColor = UIColor.red
但我不知道如何更改主气泡的背景颜色(现在它是透明的 grey/white)。使用 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 文件中自定义标签的颜色,但不知道如何访问默认标题属性。
UIViewCallout
是私有 class。如果您想要自定义标注视图:
禁用标准标注
view.canShowCallout = false
使用您自定义的
UIView
实现MKMapViewDelegate
方法用于标注:func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { let redCalloutView = RedCalloutView(view.annotation) view.addSubview(redCalloutView) } func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) { view.subviews.forEach { if [=10=] is RedCalloutView { [=10=].removeFromSuperview() } } }
我已经根据您的要求创建了代码,请在下面找到 url 下载代码并查看它。
Link : https://www.dropbox.com/s/o2howwqceq8rsgu/MapInformation.zip?dl=0
Environment : Xcode 8 and Swift3
突出显示我完成的代码。
我采用了显示 Popup(UIPresentationController
) 而不是标注的方法。欲了解更多信息,请找到以下代码。
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
}
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)
}
注
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
请看下面的截图。
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
for v in view.subviews{
if v.subviews.count > 0{
v.subviews[0].backgroundColor = UIColor.red
}
}
}
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
for v in view.subviews{
if v.subviews.count > 0 {
let colloutView = v.subviews[0]
colloutView.backgroundColor = UIColor(red: 0.0/255.0, green: 0.0/255.0, blue: 0.0/255.0, alpha: 0.8)
if colloutView.subviews.count > 0 {
if colloutView.subviews[0].subviews.count > 0{
colloutView.subviews[0].subviews.forEach { (view) in
if let label = view as? UILabel{
label.textColor = UIColor.white
}
}
}
}
}
}
}
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
//design your custom view and set it to detailCalloutAccessoryView
view.detailCalloutAccessoryView = yourDetailView
detail.superview?.superview?.backgroundColor = yourColor
// This view is type of MKSmallCalloutView
}