I have created a custom annotation view. In the setSelected method i have implemented a view as i need, now i want add a button to that view. But the button not detecting the click event, it absorbed by the mapview. what should i do for adding button like rightcalloutaccessory. Thanks in advance.
Asked
Active
Viewed 1,973 times
2
-
Try the answer to [this question](http://stackoverflow.com/q/6941199/467105). – Oct 24 '11 at 15:23
1 Answers
3
I solved this problem by changing control event for the UIButton
as
[button addTarget:self action:@selector(clickEvent) forControlEvents:UIControlEventTouchDown]
Below is the part of my code to create custom annotations.
- (CustomAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[CustomAnnotation class]])
{
CustomAnnotationView *newAnnotationView = nil;
// determine the type of annotation, and produce the correct type of annotation view for it.
newAnnotationView = [[[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil delegate: targetController] autorelease];
[newAnnotationView setEnabled:YES];
[newAnnotationView setCanShowCallout:NO];
return newAnnotationView;
}
}
@interface CustomAnnotationView : MKAnnotationView
{
}
@end
@implementation CustomAnnotationView
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier delegate:(id)targetController
{
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
/* Set initial width as you want. ex: 50X50*/
self.frame = CGRectMake(0, 0, Initial_width, Initial_height);
self.backgroundColor = [UIColor redColor];
return self;
}
/* When you tap on an annotation this method is fired*/
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
if(selected){
/* When you select annotation, instead of showing default callout you can increase the width &height of annotation and add your custom view here*/
self.frame = CGRectMake(0, 0, max_width, max_height);
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(6, 0, max_width-10, max_height)];
view.backgroundColor = [UIColor blackColor];
UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *img = [UIImage imageNamed:@"image.png"];
but = CGRectMake(x , y, img.size.width + 20, img.size.height + 15);
[but setImage:img forState:UIControlStateNormal];
[but addTarget:targetController action:@selector(methodName) forControlEvents:UIControlEventTouchDown];
[view addSubview:but];
[self addSubview:view];
}
else {
/* When you tap outside of annotation, set the initial frame for the annotation. and remove the custom view from annotation*/
self.frame = CGRectMake(0, 0, Initial_width, Initial_height);
[view removeFromSuperview];
}
}

san
- 295
- 2
- 18
-
How did you stop the touch event form bubbling up to the `mapView:didSelectAnnotationView:` function? – jlindenbaum Dec 03 '12 at 19:40
-
@san can you help me out with this. this code doesn't work for me. – Muhammad Umar Mar 05 '13 at 19:22
-
-
@san by increasing the self.frame the the pin you are using won't change its size? – Muhammad Umar Mar 11 '13 at 18:07
-
@MuhammadUmar Download the sample code from [github](https://github.com/sanios/IOS_MapAnnotations) – san Mar 14 '13 at 08:00
-