如何使用detailCalloutAccessoryView更改标注气泡的默认背景颜色 [英] How to change default background color of callout bubble with detailCalloutAccessoryView

查看:404
本文介绍了如何使用detailCalloutAccessoryView更改标注气泡的默认背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我进行了以下缝合.

In my app I have the following sutuation.

我已经实现了带有自定义detailCalloutAccessoryView的自定义标注气泡,其中带有两个标签.

I've implemented a custom callout bubble with custom detailCalloutAccessoryView with two labels inside.

我知道如何用此行更改detailCalloutAccessoryView的颜色.

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

view.detailCalloutAccessoryView?.backgroundColor = UIColor.red

但是我不知道如何更改主气泡的背景颜色(现在是透明的灰色/白色).在view.detailCalloutAccessoryView?.backgroundColor = UIColor.red行中,我的标注气泡看起来像这样:

But I can't figure out how to change background color of the main bubble (it is transparent grey/white now). With view.detailCalloutAccessoryView?.backgroundColor = UIColor.red line my calloutbubble looks like this:

但是我希望我的自定义气泡看起来像这样:

But I want my custom bubble to look like this:

这是我的viewFor注释方法:

Here is my viewFor 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
    }

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

I'm working in Xcode 8 w/ Swift 3.

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

It would be also interesting to know how to change font type and default black color of the title from black to another color. In detail view i can easily change color of my custom labels in xib file but don't know how to access default title properties.

推荐答案

我已根据您的要求创建了代码,请找到以下网址以下载代码并进行审查.

I had created the code for your requirement please find the below url for download the code and review it.

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

环境:Xcode 8和Swift3

Environment : Xcode 8 and Swift3

突出显示我完成的代码.

我采用了显示Popup(UIPresentationController)而不是标注的方法.有关更多信息,请找到以下代码.

I had taken the approach to display the Popup(UIPresentationController) instead of callout. For more information please find the below code.

A)我曾经使用UIButton在MapView上显示为注释,并在用户单击它时显示弹出窗口.

A) I had used the UIButton to display as annotation on the MapView and display the popup when user click on it.

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)当用户单击注释时显示弹出窗口.

B) Display the popup when user click on the annotation.

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

注意

Note

如果要将弹出窗口的颜色从红色更改为其他颜色 颜色,那么您只能通过更改颜色名称来进行单行编码.

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

popupVC?.popoverPresentationController?.backgroundColor = UIColor.red

请查看以下屏幕截图.

这篇关于如何使用detailCalloutAccessoryView更改标注气泡的默认背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆