How to change the default background color for an external bubble using detailCalloutAccessoryView

In my application, I have the following line.

I have implemented a custom call bubble with a custom detailCalloutAccessoryView with two shortcuts inside.

I know how to change the color of detailCalloutAccessoryView with this line.

view.detailCalloutAccessoryView?.backgroundColor = UIColor.red

But I can’t understand how to change the background color of the main bubble (now it is transparent gray / white). With a line, view.detailCalloutAccessoryView?.backgroundColor = UIColor.redmy calloutbubble looks like this:

enter image description here

But I want my custom bubble to look like this:

enter image description here

Here is my view for the annotation method:

 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
    }

I work in Xcode 8 w / Swift 3.

, . xib , , .

+4
2

, , URL- .

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

: Xcode 8 Swift3

, .

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

, , .

popupVC?.popoverPresentationController?.backgroundColor = UIColor.red

.

enter image description here

+1

UIViewCallout - . , :

  • view.canShowCallout = false

  • MKMapViewDelegate UIView :

    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()
            }
        }
    }
    
+2

Source: https://habr.com/ru/post/1654446/


All Articles