1. <tfoot id='0kLPQ'></tfoot>

    <small id='0kLPQ'></small><noframes id='0kLPQ'>

    <i id='0kLPQ'><tr id='0kLPQ'><dt id='0kLPQ'><q id='0kLPQ'><span id='0kLPQ'><b id='0kLPQ'><form id='0kLPQ'><ins id='0kLPQ'></ins><ul id='0kLPQ'></ul><sub id='0kLPQ'></sub></form><legend id='0kLPQ'></legend><bdo id='0kLPQ'><pre id='0kLPQ'><center id='0kLPQ'></center></pre></bdo></b><th id='0kLPQ'></th></span></q></dt></tr></i><div id='0kLPQ'><tfoot id='0kLPQ'></tfoot><dl id='0kLPQ'><fieldset id='0kLPQ'></fieldset></dl></div>

    1. <legend id='0kLPQ'><style id='0kLPQ'><dir id='0kLPQ'><q id='0kLPQ'></q></dir></style></legend>
        <bdo id='0kLPQ'></bdo><ul id='0kLPQ'></ul>

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

      时间:2023-09-10

          • <bdo id='5fU5R'></bdo><ul id='5fU5R'></ul>

              • <tfoot id='5fU5R'></tfoot>
                <i id='5fU5R'><tr id='5fU5R'><dt id='5fU5R'><q id='5fU5R'><span id='5fU5R'><b id='5fU5R'><form id='5fU5R'><ins id='5fU5R'></ins><ul id='5fU5R'></ul><sub id='5fU5R'></sub></form><legend id='5fU5R'></legend><bdo id='5fU5R'><pre id='5fU5R'><center id='5fU5R'></center></pre></bdo></b><th id='5fU5R'></th></span></q></dt></tr></i><div id='5fU5R'><tfoot id='5fU5R'></tfoot><dl id='5fU5R'><fieldset id='5fU5R'></fieldset></dl></div>
                <legend id='5fU5R'><style id='5fU5R'><dir id='5fU5R'><q id='5fU5R'></q></dir></style></legend>

                  <tbody id='5fU5R'></tbody>
              • <small id='5fU5R'></small><noframes id='5fU5R'>

                本文介绍了如何使用自动布局在 MKAnnotation 中显示多行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在使用 mapkit 如何在 MKAnnotation 视图中使用多行.

                i am using mapkit how i can multiple line in MKAnnotation view.

                那里的每个注释都有标题和副标题.我如何在 auto layout 的帮助下显示多行副标题?

                Every annotation there has title and subtitle. how i show sub title with multiple line with the help of auto layout ?

                我找到了答案.请尝试我的答案.我们只需要在

                i found it's Answer .plz try my answer. we just need to do code in

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

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

                here i show the idea how to use autolayout with MKAnnotation .

                推荐答案

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

                we can show multiple line in MKAnnotation view With the help of auto layout

                这很简单.

                目标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;
                    }
                

                输出

                对于斯威夫特

                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

                我以编程方式创建标签.在其上添加约束,然后在MKAnnotationView的detailCalloutAccessoryView中添加标签.

                i programatically create a label. add the constraint on it and then add the label in detailCalloutAccessoryView of MKAnnotationView.

                这篇关于如何使用自动布局在 MKAnnotation 中显示多行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:故事板和自动布局:如何制作圆形图像 下一篇:出现异常:“无法使用未准备好约束的视图层次结构设置布局"

                相关文章

                <i id='GI3GA'><tr id='GI3GA'><dt id='GI3GA'><q id='GI3GA'><span id='GI3GA'><b id='GI3GA'><form id='GI3GA'><ins id='GI3GA'></ins><ul id='GI3GA'></ul><sub id='GI3GA'></sub></form><legend id='GI3GA'></legend><bdo id='GI3GA'><pre id='GI3GA'><center id='GI3GA'></center></pre></bdo></b><th id='GI3GA'></th></span></q></dt></tr></i><div id='GI3GA'><tfoot id='GI3GA'></tfoot><dl id='GI3GA'><fieldset id='GI3GA'></fieldset></dl></div>
              • <legend id='GI3GA'><style id='GI3GA'><dir id='GI3GA'><q id='GI3GA'></q></dir></style></legend>
                  • <bdo id='GI3GA'></bdo><ul id='GI3GA'></ul>
                1. <small id='GI3GA'></small><noframes id='GI3GA'>

                  <tfoot id='GI3GA'></tfoot>