UIButtonTypeDetailDisclosure color change

I changed the image UIButtonTypeDetailDisclosureto the image below, but the problem is that the image turns blue. Where is my problem?

- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
*infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

[infoButton setImage:[UIImage imageNamed:@"carcar.png"] forState: UIControlStateNormal];

pinView.rightCalloutAccessoryView = infoButton;
} 

enter image description here

enter image description here

Last update: Solved using the latest 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;
+4
source share
5 answers

Use a custom button type:

infoButton = [UIButton buttonWithType:UIButtonTypeCustom];

If you want to add your own image, you should always use this method. Other types are for predefined buttons (e.g. information, contact, etc.).

+2
source

I had a similar problem and solved 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];
+3

, iOS 7 .

[infoButton setImage:[UIImage imageNamed:@"carcar.png"] forState: UIControlStateNormal];

UIImage *carImage = [UIImage imageNamed:@"carcar.png"];
if ([carImage respondsToSelector:@selector(imageWithRenderingMode:)])
{
    carImage = [carImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}

[infoButton setImage:carImage forState: UIControlStateNormal];
+1

.

You do not see anything, because the frame of your button is zero (for UIButtonTypeCustom). So, 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];
0
source

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;
0
source

All Articles