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

标签 ios objective-c swift autolayout mapkit

enter image description here

我正在使用 mapkit 如何在 MKAnnotation View 中显示多行。

那里的每个注释都有标题和副标题。 我如何在自动布局的帮助下显示多行副标题?

我发现它是答案。请尝试我的答案。我们只需要在

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

}

在这里我展示了如何将自动布局与 MKAnnotation 一起使用。

最佳答案

借助自动布局,我们可以在 MKAnnotation View 中显示多行

非常简单。

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

输出

enter image description here

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


}

enter image description here

这里我使用NSLayoutConstraint

我以编程方式创建一个标签。在上面添加约束,然后在MKAnnotationView的detailCalloutAccessoryView中添加label。

关于ios - 如何使用自动布局在 MKAnnotation 中显示多行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40214778/

相关文章:

ios - 如何将视频转码为16 :9 format and vary the bit rate (and perhaps FPS)?

iphone - 在 IOS 5 中从 UIImagePickerController 保存视频

objective-c - bundle ID中的$ product是什么

ios - 检索核心数据关系的计数

swift 无法改变 uiview 的宽度

objective-c - 使 UITabBar 项目看起来都未选中

ios - 尝试将 AdWhirl 与 Admob 结合使用时出错

IOS/Objective-c : Access a ViewController with a button, "opposing" Storyboard结构

ios - 在 Swift 中从多个服务解码 JSON 的更简单方法

ios - swift 2.0 中的关闭声明