ios - map 标注 showCallout-iOS

标签 ios ios6 mkmapview ios6-maps

我在显示注释标题时遇到问题,如下图所示。第一张图片很好地表示了值(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/

相关文章:

ios6 - iOS 6 GKMatchRequest playersToInvite 失败,GKTurnBasedParticipant 中的 playerID 为 nil(适用于 iOS5)

ios - 支持 iOS 6 时,LaunchImage 和背景图像未对齐 3.5"

iphone - 移动 MKMapView 点到像素坐标

ios - 当用户更改联系人访问权限时,iOS 6 中的应用程序崩溃

iphone - MKMapKit 实际最大 MKPolylines 数

iphone - UIView,如何判断触摸何时进入 View

ios - Swift - 当应用程序进入后台时去查看

objective-c - NSProxy 类有用的真实例子,为什么?

iphone - 以编程方式重置 UISearchbar

javascript - 过渡 react 导航标题背景颜色