Annotation showCallout-iOS Card

I have a problem to show the annotation title as shown in the following images. The first image means very good value; on the other hand, as soon as the value reaches three digits, then the header displays three dots, as shown in the second image. I would like to know how to fix this problem. Any idea would be more than welcome !. Thanks a lot in advance, appreciated! I just put my code here for reference!

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

My showCallout header is updated in the following code:

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

enter image description hereenter image description here

+4
source share
2 answers

The problem is that the layout of the leader view is not recounted when the name changes. Maybe you should specify an error report for this.

To force a relay, you can deselect and programmatically select the annotation if the leader is visible. It is not animated, but changes the width of the leader:

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

You should also delete this line, since the annotation header is used anyway as a text label in the annotation:

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

UPDATE:

The @detunized solution provides animation work. Instead of creating an unnecessary UIView, you can better remove and re-add the button 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; } } 
+2
source

I do not know the correct solution to this problem, but I have a hacker for you. You need to trigger a view relay, and this can be achieved by messing around with the accessory. You do not use the left accessory here, so you can install it and install it on nil . Like this:

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

I tried and works on iOS 5. I can not test on iOS 6.

If you do this often, then it's good not to create a UIView all the time, but rather reuse something.

I had the same problem when I was updating rightCalloutAccessoryView . It only worked if I first set it to nil and then immediately set it to everything I need in the first place.

0
source

Source: https://habr.com/ru/post/1444691/


All Articles