how to show multiple lines in MKAnnotation with autolayout?(如何使用自动布局在 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 中显示多行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用自动布局在 MKAnnotation 中显示多行?
				
        
 
            
        基础教程推荐
- SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
 - UIImage 在开始时不适合 UIScrollView 2022-01-01
 - navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
 - Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
 - iOS - UINavigationController 添加多个正确的项目? 2022-01-01
 - 如何比较两个 NSDate:哪个是最近的? 2022-01-01
 - 如何将图像从一项活动发送到另一项活动? 2022-01-01
 - 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
 - Play 商店的设备兼容性问题 2022-01-01
 - Android Volley - 如何动画图像加载? 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
				
				
				
				