环球银行金融电信协会2 : multiline MKPointAnnotation

标签 swift user-interface mapkit mapkitannotation

有没有办法创建多行注释?

这是我的代码:

1)我的customAnnotation类

import UIKit
import MapKit

class CustomAnnotation: NSObject, MKAnnotation {

    var title: String?
    var subtitle: String?
    var address: String = ""
    var phoneNumber: String = ""
    var workingHours: String = ""
    var coordinate: CLLocationCoordinate2D


    init(   title: String,
            subtitle: String,
            address: String,
            phoneNumber: String,
            workingHours: String,
            coordinate: CLLocationCoordinate2D ){

        self.title = title
        self.subtitle = subtitle
        self.address = address
        self.phoneNumber = phoneNumber
        self.workingHours = workingHours
        self.coordinate = coordinate

        super.init()
    }
}

2)我的 View Controller 与 map :

MapViewController 类:UIViewController、MKMapViewDelegate {

@IBOutlet weak var theMapView: MKMapView!

override func viewDidLoad() {
    super.viewDidLoad()


            let latitude: CLLocationDegrees = 54.685290
            let longitude : CLLocationDegrees = 25.279661

            let latDelta : CLLocationDegrees = 0.2
            let lonDelta : CLLocationDegrees = 0.2

            let theSpan : MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)

            let branchLocation : CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)

            let theRegion : MKCoordinateRegion = MKCoordinateRegionMake(branchLocation, theSpan)


            let annotation = CustomAnnotation(title: "Brach1", subtitle: "some text for B1", address: "address str 12-12", phoneNumber: "123 321 123", workingHours: "00 - 24", coordinate: branchLocation)


            self.theMapView.setRegion(theRegion, animated: true)

            self.theMapView.addAnnotation(annotation)

        }


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}


func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

    let identifier = "MyPin"

    if annotation.isKindOfClass(MKUserLocation) {
        return nil
    }

    var annotationView: MKPinAnnotationView? = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) as? MKPinAnnotationView

    if annotationView == nil {

        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
        annotationView?.canShowCallout = true

        let multiLineView = UIView(frame: CGRectMake(0, 0, 100, 40))

        let label1 = UILabel(frame: CGRectMake(0,0,100,20))
        label1.text = "Some text1"
        multiLineView.addSubview(label1)

        let label2 = UILabel(frame: CGRectMake(0,20,100,20))
        label2.text = "some text2"
        multiLineView.addSubview(label2)

        annotationView!.rightCalloutAccessoryView = multiLineView

    } else {
        annotationView!.annotation = annotation
    }
return annotationView
}

}

我得到了什么:

enter image description here

如果我会使用

annotationView!.leftCalloutAccessoryView = multiLineView

我会得到:

enter image description here

我还没有找到其他方法。我希望像bottomCaouttAccessoryView这样的东西得到类似的东西:

enter image description here

这对我来说非常重要,注释的高度不可修改。 (这就是为什么我的三个标签甚至不适合那里) 我试图使用注释 View 的自动调整大小方法,但没有运气:(

最佳答案

副标题是一个打印到单行标签的字符串,您可能必须创建一个子类才能添加多行。

关于环球银行金融电信协会2 : multiline MKPointAnnotation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37446219/

相关文章:

swift - 在 Swift 3 中将数据写入 NSOutputStream

css - Qt 可以创建类似 Blender 的 GUI 吗?

java - 我的 Java GUI 中已突出显示按钮

ios - 如何根据我的 MKMapView 图片适本地调整缩放级别?

ios - foursquare 如何将其 map View 从表格标题动画化为全屏?

ios - rawValue 属性在哪里?

ios - AFNetworking - HTTP 加载失败(错误代码 : -999) swift iOS

ios - 数据被检索为project.model而不是swift ios中的元素

swing - Clojure:窗口框架关闭时退出程序

ios - 使用 MapKit 和 SwiftUI、Xcode 12 在默认 iOS map 上移动 View 时如何修复终端输出 "Style Z is requested for an invisible rect"