objective-cuibuttonmkannotationviewcallouts

setting background on UIButton on Map Callout


Hi I am trying to set a background image on my detail disclosure UIButton attached to my MapView MKAnnotation callout view. Checking on stackoverflow there are consistent examples of how to set this, however it doesn't seem to work for my callout UIButton. My question is, does this not work, simply because of the mapview or am I missing something else? Here is my code:

// If an existing pin view was not available, create one.
            pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];
            pinView.canShowCallout = YES;
            pinView.image = [UIImage imageNamed:@"Tch4"];
            //pinView.calloutOffset = CGPointMake(0, 32);
            pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            UIImage *btnImage = [UIImage imageNamed:@"4stars"];
            [pinView.rightCalloutAccessoryView setBackgroundImage:btnImage forState:UIControlStateNormal];

I receive the error on the final line. Error is: No Visable interface for UIView declares the selector 'setbackgroundimage: forstate'

Thanks for your help with this


Solution

  • Your problem is rightCalloutAccessoryView returns a UIView instance. And UIView does not respond to setBackgroundImage:forState: method.

    What you are looking for is to add a UIButton instead something like this:

    UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    infoButton.frame = CGRectMake(0, 0, 32, 32); // Set your own frame 
    [infoButton setBackgroundImage:btnImage forState:UIControlStateNormal];
    [infoButton setBackgroundImage:btnImage forState:UIControlStateSelected];
    pinView.rightCalloutAccessoryView = infoButton;