有没有一种方法可以创建多行注释?
下面是我的代码:
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)我的带有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
}}
我得到的是:

如果我要使用
annotationView!.leftCalloutAccessoryView = multiLineView我会得到:

我没有找到任何其他的方法。我希望像bottomCalouttAccessoryView这样的东西会得到这样的结果:

对于我来说,注释的高度是不可修改的,这对我来说是非常有意义的。(这就是为什么我的三个标签甚至不适合那里)我试图尝试自动调整注解视图的大小方法,但没有成功:(
发布于 2016-05-26 03:43:09
副标题是打印到单行标签的字符串,您可能必须创建一个子类才能添加多行。
发布于 2016-05-26 06:04:22
尤其是这部分(我帮你把它转换成了Swift )
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}
发布于 2016-09-09 12:15:09
请使用我以编程方式设置的NSLayoutConstraint。
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 label1 = UILabel(frame: CGRectMake(0, 0, 200, 21))
label1.text = "Some text1 some text2 some text2 some text2 some text2 some text2 some text2"
label1.numberOfLines = 0
annotationView!.detailCalloutAccessoryView = label1;
let width = NSLayoutConstraint(item: label1, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.LessThanOrEqual, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 200)
label1.addConstraint(width)
let height = NSLayoutConstraint(item: label1, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90)
label1.addConstraint(height)
} else {
annotationView!.annotation = annotation
}
return annotationView
}
}和输出

https://stackoverflow.com/questions/37446219
复制相似问题