地图标注showCallout-iOS

4

我有问题需要展示注释标题,如下图所示。第一张图片非常清晰地表示了其值;然而,当值达到三位数时,标题会显示为三个点,如第二张图片所示。我想知道如何解决这个问题。任何建议都将不胜感激!提前感谢您的帮助!我在此只是放置了代码供参考!

- (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


你使用的是哪个版本的iOS? - detunized
嗨@user1724168,我的下面的答案在iOS 6上模拟器和设备上都有效。 :) - Tammo Freese
我明天早上一定会检查,现在我没有带Mac。 - user1724168
2个回答

2
问题在于当标题更改时,呼出视图的布局没有重新计算。也许你应该为此提交一个错误报告。
如果呼出视图可见,可以通过以编程方式取消选择和选择注释来强制重新布局。它不是动画效果,但它会更改呼出视图的宽度。
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,您可以更好地删除并重新添加按钮视图:

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

0

我不知道这个问题的正确解决方案,但是我有一个hacky的解决方法。您需要触发视图的重新布局,可以通过操作辅助视图来实现。在这里,您没有使用左边的辅助视图,因此可以将其设置为某些内容,然后再将其设置回 nil 。像这样:

self.pin.title = @"Ozone Level: 100";
self.pin_view.leftCalloutAccessoryView = [[[UIView alloc] init] autorelease];
self.pin_view.leftCalloutAccessoryView = nil;

我已经尝试过在iOS 5上运行,但无法在iOS 6上测试。

如果您经常这样做,那么最好不要每次都创建UIView,而是重复使用某些内容。

当我更新rightCalloutAccessoryView时,我遇到了同样的问题。只有在我首先将其设置为nil,然后立即将其设置为我需要的任何内容时,它才能正常工作。


@user1724168,你在其他地方更新了 title 吗?你发布的代码显示了你创建注释的位置。你的问题出现在稍后更新文本的时候。你需要将我的 hack 移动到那个地方。在这里你不需要它。 - detunized
请查看最新代码,位于底部,只有两行代码! - user1724168
@user1724168,在此之后,请尝试添加我的两行代码。这应该会有所帮助。 - detunized
它仍然给我相同的错误。annotation.title = [NSString stringWithFormat:@"臭氧水平:%@",[attr2 stringValue]]; pinView.leftCalloutAccessoryView = [[UIView alloc] init]; pinView.leftCalloutAccessoryView = nil; - user1724168
那么这个黑客攻击不再适用于iOS 6。抱歉。 - detunized

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接