如何使用自动布局在MKAnnotation中显示多行?

在这里输入图像说明

我正在使用mapkit,我怎么可以在MKAnnotation视图中多行。

每个注释都有标题和副标题。 如何显示与多行自动布局的帮助子标题?

我发现它的答案.plz试试我的答案。 我们只需要做代码

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { } 

在这里我展示了如何使用MKAnnotation自动布局的想法。

我们可以在MKAnnotation视图中显示多行使用自动布局的帮助

这很简单。

目标c

  - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { if ([annotation isKindOfClass:[MKUserLocation class]]) return nil; if ([annotation isKindOfClass:[CustomAnnotation class]]) { CustomAnnotation *customAnnotation = (CustomAnnotation *) annotation; MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomAnnotation"]; if (annotationView == nil) annotationView = customAnnotation.annotationView; else annotationView.annotation = annotation; //Adding multiline subtitle code UILabel *subTitlelbl = [[UILabel alloc]init]; subTitlelbl.text = @"sri ganganagar this is my home twon.sri ganganagar this is my home twon.sri ganganagar this is my home twon. "; annotationView.detailCalloutAccessoryView = subTitlelbl; NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:subTitlelbl attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationLessThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:150]; NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:subTitlelbl attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:0]; [subTitlelbl setNumberOfLines:0]; [subTitlelbl addConstraint:width]; [subTitlelbl addConstraint:height]; return annotationView; } else return nil; } 

产量

在这里输入图像说明

对于Swift

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

在这里输入图像说明

在这里我使用NSLayoutConstraint

我编程创build一个标签。 在它上面添加约束,然后在MKAnnotationView的detailCalloutAccessoryView中添加标签。

添加多行的扩展名:

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

使用:

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