drawRect not called on MKPinAnnotationView subclass
I implemented a class that extends MKPinAnnotationView. I want to draw something below the pin, which I hope to achieve by taking over the drawRect:rect message. I want to do this by painting something myself first and then chaining to the superclass.
The problem is that this message doesn't event get sent. I already tried setting the frame size to something not empty or nil (the classic cause) without any effect. Could the implementation of the MKPinAnnotationView somehow cause the drawRect:rect message to not be sent to subclasses?
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface QueryAnnotationView : MKPinAnnotationView {
}
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier;
@end
Implementation :
#import "QueryAnnotationView.h"
@implementation QueryAnnotationView
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier;
{
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if (self)
{
self.frame = CGRectMake(0, 0, 65, 100);
}
return self;
}
- (void)drawRect:(CGRect)rect
{
NSLog(@"drawing my ow开发者_如何学编程n stuff..");
[super drawRect:rect];
}
@end
Try this:
in your (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
add this two lines at the end of if (self) block:
self.backgroundColor = any color but clear color (e.g. redColor)
self.backgroundColor = [UIColor clearColor];
This works on my code, but I don't know why.
Also, you can try this:
- (void)setAnnotation:(id <MKAnnotation>)annotation
{
[super setAnnotation:annotation];
[self setNeedsDisplay];
}
精彩评论