gpt4 book ai didi

ios - map 标注 showCallout-iOS

转载 作者:可可西里 更新时间:2023-11-01 05:56:52 29 4
gpt4 key购买 nike

我在显示注释标题时遇到问题,如下图所示。第一张图片很好地表示了值(value);另一方面,一旦值上升到三位数,标题就会显示三个点,如第二张图片所示。我想知道如何解决这个问题。任何想法都将非常受欢迎!非常感谢,感谢!我刚刚把我的代码放在这里以供引用!

- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation {
MKAnnotationView *pinView=nil;
if(![annotation isKindOfClass:[Annotation class]]) // Don't mess user location
return nil;

static NSString *defaultPinID = @"StandardIdentifier";
pinView = (MKAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if (pinView == nil){
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID];
}

if ([annotation isKindOfClass:[Annotation class]]) {
Annotation *a = (Annotation *)annotation;

pinView.image = [ZSPinAnnotation pinAnnotationWithColor:a.color];
pinView.annotation = a;
pinView.enabled = YES;
pinView.centerOffset=CGPointMake(6.5,-16);
pinView.calloutOffset = CGPointMake(-11,0);

}

pinView.canShowCallout = YES;
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
[pinView setRightCalloutAccessoryView:rightButton];
pinView.leftCalloutAccessoryView = [[UIView alloc] init];
pinView.leftCalloutAccessoryView=nil;

return pinView;
}

我的 showCallout 标题在以下代码中更新:

NSNumber *attr2=[attr valueForKey:@"ozone_level"];
annotation.title=[NSString stringWithFormat:@"Ozone Level:%@",[attr2 stringValue]];

enter image description here enter image description here

最佳答案

问题是当标题改变时标注 View 的布局没有重新计算。也许您应该为此提交错误报告。

要强制重新布局,您可以取消选择并以编程方式选择注释(如果标注可见)。它不是动画,但会改变标注的宽度:

NSNumber *attr2=[attr valueForKey:@"ozone_level"];
annotation.title=[NSString stringWithFormat:@"Ozone Level:%@",[attr2 stringValue]];


if ([self.mapView viewForAnnotation:annotation] != nil) {
NSArray *selectedAnnotations = self.mapView.selectedAnnotations;
if ((selectedAnnotations != nil) && ([self.mapView.selectedAnnotations indexOfObject:annotation] != NSNotFound)) {
[self.mapView deselectAnnotation:annotation animated:NO];
[self.mapView selectAnnotation:annotation animated:NO];
}
}

你也应该删除这一行,因为注释的标题无论如何都用作注释上的文本标签:

[rightButton setTitle:annotation.title forState:UIControlStateNormal];

更新:

@detunized 提供的解决方案适用于动画。不过,与其创建不需要的 UIView,不如删除并重新添加按钮 View :

NSNumber *attr2=[attr valueForKey:@"ozone_level"];
annotation.title=[NSString stringWithFormat:@"Ozone Level:%@",[attr2 stringValue]];

MKAnnotationView *annotationView = [self.mapView viewForAnnotation:annotation];
// The annotation must be visible, otherwise a refresh isn't needed
if (annotationView != nil) {
NSArray *selectedAnnotations = self.mapView.selectedAnnotations;
// The annotation must be selected, otherwise a refresh isn't needed
if ((selectedAnnotations != nil) && ([self.mapView.selectedAnnotations indexOfObject:annotation] != NSNotFound)) {
// Unset and re-set the right button to force relayout
UIView *view = annotationView.rightCalloutAccessoryView;
annotationView.rightCalloutAccessoryView = nil;
annotationView.rightCalloutAccessoryView = view;
}
}

关于ios - map 标注 showCallout-iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13280221/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com