ios - 使用 xib 创建自定义 MKAnnotationView

标签 ios objective-c iphone mkmapview mkannotationview

我想在我的 map 上显示由 XIB 创建的自定义 MKAnnotationView,但是当我加载注释时,我在 map 上看不到它。

这是代码:

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]只需返回与 xib 相关的 View (此方法效果很好)

这是与 mapView:viewForAnnotation: 相关的代码委托(delegate)方法
- (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;
    }
}

有任何想法吗?

最佳答案

我在 MapView 类上有自定义注释类 CallOutAnnotationVifew :

在 .h 文件中,

#import <MapKit/MapKit.h>

@interface CallOutAnnotationVifew : MKAnnotationView 

@property (nonatomic,retain)UIView *contentView;
@end

在 .m 文件中,
#import "CallOutAnnotationVifew.h"
#import <QuartzCore/QuartzCore.h>


#define  Arror_height 15

@interface CallOutAnnotationVifew ()

-(void)drawInContext:(CGContextRef)context;
- (void)getDrawPath:(CGContextRef)context;
@end

@implementation CallOutAnnotationVifew
@synthesize contentView;

- (void)dealloc
{
    self.contentView = nil;
    [super dealloc];
}

- (id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.canShowCallout = NO;
        self.centerOffset = CGPointMake(0, -120);
        self.frame = CGRectMake(0, 0, 250, 220);

        UIView *_contentView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - Arror_height)] autorelease];
        _contentView.backgroundColor   = [UIColor clearColor];
        [self addSubview:_contentView];
        self.contentView = _contentView;

    }
    return self;
}
@end

在 ViewController 中添加 map View 的代码如下。
self.mapView.delegate = self;
self.mapView.showsUserLocation = true;
self.mapView.pitchEnabled = true;
self.mapView.showsBuildings = true;

加载自定义注释 XIB View 的代码如下,这是直接添加到您的 mapView
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    CallOutAnnotationVifew *anotationCua = [[CallOutAnnotationVifew alloc] initWithAnnotation:annotation reuseIdentifier:@"CallOutAnnotationVifew"];

    NSArray *ary = [[NSBundle mainBundle] loadNibNamed:@"Display" owner:self options:nil];

    UIView *view1 = [ary objectAtIndex:0];

    [anotationCua.contentView addSubview:view1];

    return anotationCua;
}

见输出:

enter image description here

关于ios - 使用 xib 创建自定义 MKAnnotationView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32714221/

相关文章:

ios - 使用重复规则将 EKAlarm 添加到 EKReminder

iphone - UIImageViews 触摸时执行操作

iphone - 即使应用程序未运行,如何调用方法? iOS

ios - 如何使由 UIPushBehavior 操纵的 View 不旋转太多?这是一个荒谬的数量

iPhone glShader二进制

iphone - NIAttributedLabel settextcolor 不工作

ios - Binary Rejected 用于导入 Health 工具包但未实现/使用该工具包

IOS:代码签名错误

iphone - 在 Core Data 中插入新的托管对象

iphone - 在后台模式下未收到 iOS APNS