问题
I am trying to change my pin colour to purple, when I do it I lose the title though. Code is:
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBarHidden=YES;
//init the location manager
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager requestWhenInUseAuthorization];
self.mapView.showsUserLocation=YES;
self.userGeoPoint=self.message[@"userLocation"];
self.pinView = [[MKPointAnnotation alloc] init];
self.pinView.title=self.message[@"fromUser"];
self.coord = CLLocationCoordinate2DMake(self.userGeoPoint.latitude, self.userGeoPoint.longitude);
self.pinView.coordinate=self.coord;
//use a slight delay for more dramtic zooming
[self performSelector:@selector(addPin) withObject:nil afterDelay:0.5];
}
-(void)addPin{
[self.mapView addAnnotation:self.pinView];
[self.mapView selectAnnotation:self.pinView animated:YES];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(self.coord, 800, 800);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotationPoint
{
if ([annotationPoint isKindOfClass:[MKUserLocation class]])//keep the user as default
return nil;
static NSString *annotationIdentifier = @"annotationIdentifier";
MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotationPoint reuseIdentifier:annotationIdentifier];
pinView.pinColor = MKPinAnnotationColorPurple;
//now we can throw an image in there
return pinView;
}
I tried setting the title property for MKPinAnnotation but there isn't one. Is there anyway I can get around this?
回答1:
In viewForAnnotation
, you need to set canShowCallout
to YES
(it's NO
by default):
pinView.pinColor = MKPinAnnotationColorPurple;
pinView.canShowCallout = YES;
A couple of unrelated points:
- It looks like you have a property named
pinView
of typeMKPointAnnotation
that you are using to create the annotation object inviewDidLoad
. Then inviewForAnnotation
, you have a local variable also namedpinView
of typeMKPinAnnotationView
. Although there is no naming conflict here, it causes and implies some conceptual confusion.MKPointAnnotation
is an annotation model class whileMKPinAnnotationView
is an annotation view class -- they are completely different things. It would be better to name the annotation propertypin
oruserAnnotation
for example. This comment in
viewForAnnotation
://now we can throw an image in there
seems to imply that you could set a custom image in the view at this point. Setting a custom image in a
MKPinAnnotationView
is not recommended since that class is designed to display default pin images in one of three colors. To use a custom image, create a plainMKAnnotationView
instead.
回答2:
The pinColor
property was deprecated in iOS 9. From iOS 9 onwards you should use pinTintColor
which takes a UIColor
rather than a MKPinAnnotationColor
.
Old:
MKPinAnnotationView *view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
view.pinColor = MKPinAnnotationColorPurple;
New:
MKPinAnnotationView *view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
view.pinTintColor = [UI Color purpleColor];
There is more information available in the Apple docs.
回答3:
I just upgrade to ios 10, apple changed the api.
pinColor no longer in use, instead, use tintColor
New:
MKPinAnnotationView *view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
view.tintColor = [UIColor purpleColor];
回答4:
Note that the pin title will be displayed when the user taps it:
If the value of this property is true, a standard callout bubble is shown when the user taps a selected annotation view. The callout uses the title and subtitle text from the associated annotation object. ( https://developer.apple.com/documentation/mapkit/mkannotationview/1452451-canshowcallout )
来源:https://stackoverflow.com/questions/29315590/trying-to-change-mkpointannotation-colour-but-losing-the-title