1
我想在我的地圖上顯示由XIB創建的自定義MKAnnotationView,但是當我加載註釋時,我沒有在地圖上看到它。用xib創建自定義MKAnnotationView
這是代碼:
FPMapPin.h
#import <MapKit/MapKit.h>
@interface FPMapPin : MKAnnotationView
@property (nonatomic,strong) IBOutlet UIImageView *imageView;
@property (nonatomic,strong) IBOutlet UILabel *labelText;
@end
FMMapPin.m
#import "FPMapPin.h"
@implementation FPMapPin
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[self setup];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
[self setup];
}
return self;
}
- (void)setup
{
UIView* view = [FPCommonUtils firstViewInNibNamed:NSStringFromClass(self.class) nibOwner:self];
view.backgroundColor = [UIColor clearColor];
view.frame = self.bounds;
[self addSubview:view];
}
注:[FPCommonUtils firstViewInNibNamed:NSStringFromClass(self.class) nibOwner:self]
簡單地返回相關的廈門國際銀行(這種方法效果非常好)
這是代碼視圖與mapView:viewForAnnotation:
代表方法
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation
{
if (annotation == mapView.userLocation)
{
return nil;
}
else
{
NSLog(@"%@",NSStringFromClass([annotation class]));
Pin *pin = (Pin*)annotation;
FPMapPin *mapPin = [[FPMapPin alloc] initWithAnnotation:annotation reuseIdentifier:@"MapPin"];
mapPin.imageView.image = [UIImage imageNamed:@"pin-white"];
mapPin.labelText.text = [NSString stringWithFormat:@"%li", pin.pinTag];
return mapPin;
}
}
任何想法?