how to show multiple lines in MKAnnotation with autolayout?

后端 未结 6 1904
臣服心动
臣服心动 2020-12-30 17:21

i am using mapkit how i can multiple line in MKAnnotation view.

Every annotation there has title and subtitle. how i show sub title with

6条回答
  •  情书的邮戳
    2020-12-30 18:15

    extension for adding multiple lines:

    import MapKit // must import MapKit for MKAnnotationView to get recognized
    
    extension MKAnnotationView {
    
        func loadCustomLines(customLines: [String]) {
            let stackView = self.stackView()
            for line in customLines {
                let label = UILabel()
                label.text = line
                stackView.addArrangedSubview(label)
            }
            self.detailCalloutAccessoryView = stackView
        }
    
    
    
        private func stackView() -> UIStackView {
            let stackView = UIStackView()
            stackView.axis = .vertical
            stackView.distribution = .fillEqually
            stackView.alignment = .fill
            return stackView
        }
    }
    

    using:

    view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
    view.canShowCallout = true
    view.loadCustomLines(customLines: ["qqqq", "wwww", "eee"])
    

提交回复
热议问题