Voici une solution qui reproduit avec précision l'image UIBarButtonItem
qui est autrement acquise en utilisant le type de système UIBarButtonSystemItemAction
.À titre d'exemple, le nouveau UIButton est inséré dans un MKAnnotationView
:
Créer un fichier de catégorie qui contient cette méthode:
@implementation UIImage (Custom)
+ (UIImage *)actionButtonImage
{
CGRect rect = CGRectMake(0, 0, 20, 27);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
[[UIColor colorWithRed:3/255.0 green:122/255.0 blue:1 alpha:1] set];
UIBezierPath *path = [UIBezierPath bezierPath];
// Box
[path moveToPoint:CGPointMake(7, 8)];
[path addLineToPoint:CGPointMake(1, 8)];
[path addLineToPoint:CGPointMake(1, 26)];
[path addLineToPoint:CGPointMake(19, 26)];
[path addLineToPoint:CGPointMake(19, 8)];
[path addLineToPoint:CGPointMake(13, 8)];
// Arrow shaft
[path moveToPoint:CGPointMake(10, 17)];
[path addLineToPoint:CGPointMake(10, 1)];
// Arrow head
[path moveToPoint:CGPointMake(6, 4.5)];
[path addLineToPoint:CGPointMake(10, 0.5)];
[path addLineToPoint:CGPointMake(14, 4.5)];
[path stroke];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@end
Dans le délégué MKMapView
, ajoutez cette implémentation (adapter au choix):
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
MKPinAnnotationView *view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Item"];
view.canShowCallout = YES;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *actionImage = [UIImage actionButtonImage];
[button setImage:actionImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, actionImage.size.width, actionImage.size.height);
view.leftCalloutAccessoryView = button;
return view;
}
merci Shaggy Frog – Jagie
+1 pour "contrariwise". –