Đây là một giải pháp sao chép chính xác hình ảnh UIBarButtonItem
mà nếu không có được bằng cách sử dụng loại hệ thống UIBarButtonSystemItemAction
.Như một ví dụ, mới được tạo ra UIButton được đưa vào một MKAnnotationView
:
Tạo một file loại có chứa phương pháp này:
@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
Trong MKMapView
đại biểu, thêm thi này (thích ứng như mong muốn):
- (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;
}
nhờ Shaggy Frog – Jagie
+1 cho "contrariwise". –