加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – 如何使用detailCalloutAccessoryView更改标注气泡的默

发布时间:2020-12-14 04:34:24 所属栏目:百科 来源:网络整理
导读:在我的应用程序中,我有以下sutuation. 我已经使用自定义detailCalloutAccessoryView实现了一个自定义标注气泡,里面有两个标签. 我知道如何用这一行改变detailCalloutAccessoryView的颜色. view.detailCalloutAccessoryView?.backgroundColor = UIColor.red
在我的应用程序中,我有以下sutuation.

我已经使用自定义detailCalloutAccessoryView实现了一个自定义标注气泡,里面有两个标签.

我知道如何用这一行改变detailCalloutAccessoryView的颜色.

view.detailCalloutAccessoryView?.backgroundColor = UIColor.red

但我无法弄清楚如何改变主气泡的背景颜色(现在它是透明的灰色/白色).使用view.detailCalloutAccessoryView?.backgroundColor = UIColor.red行我的calloutbubble看起来像这样:

enter image description here

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

enter image description here

这是我的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
    }

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

请看下面的截图.

enter image description here

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读