【问题标题】:How to change default background color of callout bubble with detailCalloutAccessoryView如何使用 detailCalloutAccessoryView 更改标注气泡的默认背景颜色
【发布时间】:2016-09-13 10:30:51
【问题描述】:

在我的应用程序中,我有以下情况。

我已经使用自定义 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
    }

我正在使用带有 Swift 3 的 Xcode 8。

知道如何将字体类型和标题的默认黑色从黑色更改为另一种颜色也会很有趣。 在详细视图中,我可以轻松更改 xib 文件中自定义标签的颜色,但不知道如何访问默认标题属性。

【问题讨论】:

    标签: swift mkmapview mkannotation swift3 mkannotationview


    【解决方案1】:
       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
        }
    

    【讨论】:

    • 它对我有用,但正确的代码是: func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { //设计您的自定义视图并将其设置为 detailCalloutAccessoryView view.detailCalloutAccessoryView = yourDetailView detail. superview?view.detailCalloutAccessoryView.superview?.backgroundColor = yourColor }
    【解决方案2】:

    UIViewCallout 是一个私有类。如果您想要自定义标注视图:

    1. 禁用标准标注view.canShowCallout = false

    2. 使用您的自定义 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 $0 is RedCalloutView {
                  $0.removeFromSuperview()
              }
          }
      }
      

    【讨论】:

      【解决方案3】:

      我已经为您的要求创建了代码,请找到下面的 url 下载代码并查看它。

      链接:https://www.dropbox.com/s/o2howwqceq8rsgu/MapInformation.zip?dl=0

      环境:Xcode 8 和 Swift3

      突出显示我编写的代码。

      我采用了显示弹出窗口(@98​​7654324@)而不是标注的方法。更多信息请查看以下代码。

      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)
          }
      

      注意

      如果您想将弹出颜色从红色更改为其他不同的颜色 color 然后你可以通过改变颜色名称来做单行编码。

      popupVC?.popoverPresentationController?.backgroundColor = UIColor.red

      请看下面的截图。

      【讨论】:

        【解决方案4】:
        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
                                }
                            }
                        }
                    }
                }
            }
        }
        

        【讨论】:

        • 这通常效果很好,但有时我选择的view 还没有subviews,并且它以默认的白色背景颜色显示。您遇到过这种情况并解决了吗?
        猜你喜欢
        • 1970-01-01
        • 2022-01-15
        • 2021-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多