UIButton在Annotation的calloutView上点击失败
Posted
技术标签:
【中文标题】UIButton在Annotation的calloutView上点击失败【英文标题】:UIButton fail to be clicked on the calloutView of the Annotation 【发布时间】:2012-12-18 05:32:03 【问题描述】:我遇到的问题:我点击了按钮,但似乎是 CalloutView 收到了我的点击,而不是 UIButton。
首先解释一下: 1.“SYSUMyAnnoCalloutView”是一个 MKPinAnnotationView 2.“SYSUMyCalloutImageView”是一个 UIImageView,我初始化它并在“SYSUMyAnnoCalloutView”上添加Subview 3. UIButton 和 UILabel 在“SYSUMyCalloutImageView”中添加Subviewed "
对应代码: 1.在“SYSUMyAnnoCalloutView.m”中
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
if (selected)
[self.calloutImageView setFrame:CGRectMake(-75, -100, 230, 130)];
[self animateCalloutAppearance];
//[self.calloutImageView becomeFirstResponder];
//[self.calloutImageView.detailButton addTarget:self.calloutImageView action:@selector(clickForDetail) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.calloutImageView];
[self setCanShowCallout:NO];
else
//Remove your custom view...
[self.calloutImageView removeFromSuperview];
在“SYSUMyCalloutImageView.m”中
- (id)initWithImage:(UIImage *)image
self = [super initWithImage:image];
if (self)
//Label
CGRect boundsForLabel;
boundsForLabel.origin.x = 20;
boundsForLabel.origin.y = 50;
boundsForLabel.size.width = self.bounds.size.width - 100;
boundsForLabel.size.height = 20;
self.messageLabel = [[UILabel alloc] initWithFrame:boundsForLabel];
self.messageLabel.backgroundColor = [UIColor clearColor];
[self addSubview:self.messageLabel];
//detailButton
CGRect buttonRect;
buttonRect.origin.x = 20;
buttonRect.origin.y = 80;
buttonRect.size.width = boundsForLabel.size.width-50;
buttonRect.size.height = 30;
self.detailButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.detailButton setFrame:buttonRect];
[self.detailButton setTitle:@"Click for detail" forState:UIControlStateNormal];
self.detailButton.userInteractionEnabled = YES;
//[self.detailButton addTarget:self.superview action:@selector(clickForDetail) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.detailButton];
回归自我;
谁能解决我的问题??将不胜感激:)
【问题讨论】:
【参考方案1】:默认情况下 UIImageView 不接受用户交互。
您应该尝试在 SYSUMyCalloutImageView.m 的 initWithImage
中添加这行代码
[self setUserInteractionEnabled:YES];
【讨论】:
以上是关于UIButton在Annotation的calloutView上点击失败的主要内容,如果未能解决你的问题,请参考以下文章
如何在 UIButton 的右边缘对齐 UIButton 图像