ios – MKAnnotationView自定义按钮图像

ios – MKAnnotationView自定义按钮图像,第1张

概述当我使用以下代码时,我试图在我的MKAnnotationView上使用自定义图像,我的注释上没有图像.我已经检查了调试以确保图像正确加载到UI Image中. - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { MKAnnotationV 当我使用以下代码时,我试图在我的MKAnnotationVIEw上使用自定义图像,我的注释上没有图像.我已经检查了调试以确保图像正确加载到UI Image中.

- (MKAnnotationVIEw *)mapVIEw:(MKMapVIEw *)mapVIEw vIEwForAnnotation:(ID <MKAnnotation>)annotation {    MKAnnotationVIEw *annotationVIEw = [mapVIEw dequeueReusableAnnotationVIEwWithIDentifIEr:@"String"];    if(!annotationVIEw) {        annotationVIEw = [[MKAnnotationVIEw alloc] initWithAnnotation:annotation reuseIDentifIEr:@"String"];        UIbutton *directionbutton = [UIbutton buttonWithType:UIbuttonTypeCustom];        UIImage *directionIcon = [UIImage imagenamed:@"IconDirections"];        [directionbutton setimage:directionIcon forState:UIControlStatenormal];        annotationVIEw.rightCalloutAccessoryVIEw = directionbutton;    }    annotationVIEw.enabled = YES;    annotationVIEw.canShowCallout = YES;    return annotationVIEw;}@H_301_12@解决方法 有两个主要问题:  

>未设置自定义标注按钮的框架,使其基本上不可见.
>正在创建MKAnnotationVIEw,但未设置其图像属性(注释本身的图像 – 而不是标注按钮).这使得整个注释不可见.

对于问题1,将按钮的框架设置为适当的值.例如:

UIImage *directionIcon = [UIImage imagenamed:@"IconDirections"];directionbutton.frame =     CGRectMake(0,directionIcon.size.wIDth,directionIcon.size.height);@H_301_12@  

对于问题2,设置注释视图的图像(或创建MKPinAnnotationVIEw):

annotationVIEw.image = [UIImage imagenamed:@"SomeIcon"];@H_301_12@  

此外,您应该通过更新注释属性来正确处理视图重用.
完整的例子:

- (MKAnnotationVIEw *)mapVIEw:(MKMapVIEw *)mapVIEw vIEwForAnnotation:(ID <MKAnnotation>)annotation {        MKAnnotationVIEw *annotationVIEw = [mapVIEw dequeueReusableAnnotationVIEwWithIDentifIEr:@"String"];    if(!annotationVIEw) {        annotationVIEw = [[MKAnnotationVIEw alloc] initWithAnnotation:annotation reuseIDentifIEr:@"String"];        annotationVIEw.image = [UIImage imagenamed:@"SomeIcon"];        UIbutton *directionbutton = [UIbutton buttonWithType:UIbuttonTypeCustom];        UIImage *directionIcon = [UIImage imagenamed:@"IconDirections"];        directionbutton.frame =             CGRectMake(0,directionIcon.size.height);        [directionbutton setimage:directionIcon forState:UIControlStatenormal];        annotationVIEw.rightCalloutAccessoryVIEw = directionbutton;        annotationVIEw.enabled = YES;        annotationVIEw.canShowCallout = YES;    }    else {        //update annotation to current if re-using a vIEw        annotationVIEw.annotation = annotation;    }        return annotationVIEw;}@H_301_12@                            	          总结       

以上是内存溢出为你收集整理的ios – MKAnnotationView自定义按钮图像全部内容,希望文章能够帮你解决ios – MKAnnotationView自定义按钮图像所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1010529.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存