这里有一个解决方案,可以精确地复制使用UIBarButtonSystemItemAction
系统类型获取的UIBarButtonItem
图像。例如,新创建的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];
[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)];
[path moveToPoint:CGPointMake(10, 17)];
[path addLineToPoint:CGPointMake(10, 1)];
[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;
}