Use a custom button type:
infoButton = [UIButton buttonWithType:UIButtonTypeCustom];
If you want to add your own image, you should always use this method. The other types are for pre-defined buttons (like info, add contact, etc.)
문제
I have changed the image of UIButtonTypeDetailDisclosure
to the image below but the problem is that the image becomes in blue color. Where would be my issue?
- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
*infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[infoButton setImage:[UIImage imageNamed:@"carcar.png"] forState: UIControlStateNormal];
pinView.rightCalloutAccessoryView = infoButton;
}
Last update: Solved with last infoButton.frame.
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *carImage = [UIImage imageNamed:@"carcar.png"];
if ([carImage respondsToSelector:@selector(imageWithRenderingMode:)])
{
carImage = [carImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
[infoButton setImage:carImage forState: UIControlStateNormal];
infoButton.frame = CGRectMake(0, 0, 32, 32);
pinView.rightCalloutAccessoryView = infoButton;
해결책 2
Use a custom button type:
infoButton = [UIButton buttonWithType:UIButtonTypeCustom];
If you want to add your own image, you should always use this method. The other types are for pre-defined buttons (like info, add contact, etc.)
다른 팁
I had a similar problem and resolved it as follows (using your code):
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[infoButton setTintColor:[UIColor clearColor]];
UIImage *carImage = [[UIImage imageNamed:@"carcar.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[infoButton setBackgroundImage:carImage forState:UIControlStateNormal];
It looks like iOS 7 is treating your image as a template. Try replacing
[infoButton setImage:[UIImage imageNamed:@"carcar.png"] forState: UIControlStateNormal];
with
UIImage *carImage = [UIImage imageNamed:@"carcar.png"];
if ([carImage respondsToSelector:@selector(imageWithRenderingMode:)])
{
carImage = [carImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
[infoButton setImage:carImage forState: UIControlStateNormal];
You need to use different button type and set it's frame.
You see nothing because the frame of your button is zero (for UIButtonTypeCustom). So, in order to fix your problem you can:
infoButton = [UIButton buttonWithType:UIButtonTypeCustom];
// set image
[infoButton setImage:yourImage forState:UIControlStateNormal];
// More elegant then setting hardcoded frame
[infoButton sizeToFit];
Use this
static NSString * const identifier = @"MyCustomAnnotation";
MKAnnotationView* annotationView = [mapView1 dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView)
{
annotationView.annotation = annotation;
}
else
{
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:identifier];
}
//left image view
UIImageView *imageIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"car.png"]];
annotationView.leftCalloutAccessoryView = imageIcon;
//right button with image
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[infoButton setTintColor:[UIColor clearColor]];
UIImage *carImage = [[UIImage imageNamed:@"Search.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[infoButton setBackgroundImage:carImage forState:UIControlStateNormal];
annotationView.rightCalloutAccessoryView = infoButton;
annotationView.image = [UIImage imageNamed:@"pin_image.png"];
annotationView.canShowCallout = YES;
return annotationView;