Вот решение, которое точно копирует образ UIBarButtonItem
, полученный в противном случае с использованием системного типа UIBarButtonSystemItemAction
. Например, недавно созданный UIButton вставляется в MKAnnotationView
:
Создайте файл категории, который содержит этот метод:
@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
В делегате MKMapView
добавьте эту реализацию (адаптируйте по желанию):
- (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;
}