Aquí hay una solución que replica exactamente la imagen UIBarButtonItem
que se adquiere utilizando el tipo de sistema UIBarButtonSystemItemAction
.A modo de ejemplo, el UIButton recién creado se inserta en una MKAnnotationView
:
Crear un archivo de categoría que contiene este método:
@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
En el delegado MKMapView
, agregue esta implementación (adaptarse a medida que se desee):
- (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;
}
gracias Shaggy Frog – Jagie
+1 por "contrariwise". –