SWIFT 2:多行MKPointAnnotation [英] SWIFT 2: multiline MKPointAnnotation

查看:97
本文介绍了SWIFT 2:多行MKPointAnnotation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有创建多行注释的方法?

Is there a way to create multiline annotations?

这是我的代码:

1)我的customAnnotation类

1) My customAnnotation class

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)我的地图控制器:

2) my view controller with map:

MapViewController类:UIViewController,MKMapViewDelegate {

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

}

我得到的是:

如果我愿意使用

annotationView!.leftCalloutAccessoryView = multiLineView

我会得到:

我还没有找到其他方法.我希望诸如bottomCalouttAccessoryView之类的东西会得到诸如

I haven't found any other methods. I would expect something like bottomCalouttAccessoryView to get something like:

对我来说,注释的高度不可修改,这非常符合我的意思. (这就是为什么我的三个标签都不适合那里的原因) 我正在尝试使用批注视图的自动调整大小方法,但是运气不好:(

That is very wired for me that the height of annotation is not amendable. (that is why my three lables don't even fit there) I was trying to play with autoresize methods of annotationview but with no luck:(

推荐答案

您是否看过: 如何为MKAnnotation字幕显示2行文本,并更改​​右侧按钮的图像?

尤其是这部分(我为您将其转换为Swift)

especially this part (I converted it to Swift for you)

   var multiLineView= UIView(frame: CGRectMake(0, 0, 23, 23))
    multiLineView.addSubview(lable1)
    multiLineView.addSubview(lable2)
    annotationView.leftCalloutAccessoryView = multiLineView

更新

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

var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier("pin")

if pinView == nil {
    pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin")
    pinView!.canShowCallout = true

    // create a new View
    var multiLineView= UIView(frame: CGRectMake(0, 0, 23, 23))
    // add all the labels you need here
    let label1 = UILabel(frame: CGRectMake(0,10,10,10))
    label1.text = "Some text"
    multiLineView.addSubview(label1)
    let label2 = UILabel(frame: CGRectMake(0,20,10,10))
    label2.text = "some text"
    multiLineView.addSubview(label2)
    pinView!.leftCalloutAccessoryView = multiLineView


}
else {
    pinView!.annotation = annotation
}

return pinView

}

这篇关于SWIFT 2:多行MKPointAnnotation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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