UIButton 不工作。不是第一响应者或隐藏在视图后面
Posted
技术标签:
【中文标题】UIButton 不工作。不是第一响应者或隐藏在视图后面【英文标题】:UIButton not working. Not first responder or is hiding behind view 【发布时间】:2012-03-16 08:31:02 【问题描述】:我的 UIButton 没有响应。我认为它落后于 UIView 或者它不是第一响应者。我制作了一个自定义视图,它的子类是 MKAnnotationView。也许这是问题的一部分。我也尝试使用 userInteractionEnabled,但没有运气。有人遇到同样的问题吗?
- (id)initWithAnnotation:(id)annotation reuseIdentifier:(NSString *)reuseIdentifier
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
// Create selected and un-selected pin images
_normalPin = [UIImage imageNamed:@"map_pin.png"];
_selectedPin = [UIImage imageNamed:@"selected_map_pin.png"];
// Set current pin to unselected image
self.image = _normalPin;
// cast annotation an our expected BLAnnotation
Annotation *blAnnotation = (Annotation *)annotation;
// create title label and size to text
_titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 280, 20)];
_titleLabel.text = blAnnotation.title;
_titleLabel.textAlignment = UITextAlignmentLeft;
_titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:13];
_titleLabel.textColor = [UIColor whiteColor];
_titleLabel.shadowColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.7];
_titleLabel.shadowOffset = CGSizeMake(0, -1.0);
_titleLabel.backgroundColor = [UIColor clearColor];
_titleLabel.lineBreakMode = UILineBreakModeWordWrap;
_titleLabel.numberOfLines = 3;
CGSize titleLabelSize = [blAnnotation.title sizeWithFont: _titleLabel.font constrainedToSize: CGSizeMake(280, 600) lineBreakMode: _titleLabel.lineBreakMode];
_titleLabel.frame = CGRectMake(_titleLabel.frame.origin.x, _titleLabel.frame.origin.y, 280, titleLabelSize.height);
// create subtitle label and size to text
_subtitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, _titleLabel.frame.size.height + 8, 280, 20)];
_subtitleLabel.text = blAnnotation.subtitle;
_subtitleLabel.textAlignment = UITextAlignmentLeft;
_subtitleLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:11];
_subtitleLabel.textColor = [UIColor whiteColor];
_subtitleLabel.shadowColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.7];
_subtitleLabel.shadowOffset = CGSizeMake(0, -1.0);
_subtitleLabel.backgroundColor = [UIColor clearColor];
_subtitleLabel.lineBreakMode = UILineBreakModeWordWrap;
_subtitleLabel.numberOfLines = 4;
CGSize subtitleLabelSize = [blAnnotation.subtitle sizeWithFont: _subtitleLabel.font constrainedToSize: CGSizeMake(235, 600) lineBreakMode: _subtitleLabel.lineBreakMode];
_subtitleLabel.frame = CGRectMake(_subtitleLabel.frame.origin.x, _subtitleLabel.frame.origin.y, subtitleLabelSize.width, subtitleLabelSize.height);
// create custom button
_infoButton = [UIButton buttonWithType:UIButtonTypeCustom];
_infoButton.frame = CGRectMake(0, _titleLabel.frame.size.height + _subtitleLabel.frame.size.height + 80, 175, 50);
//_infoButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//_infoButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
//[_infoButton setImage:[UIImage imageNamed:@"more_info.png"] forState:UIControlStateNormal];
[_infoButton addTarget:self action:@selector(pressButton:) forControlEvents:UIControlEventTouchUpInside];
// create callout view to be shown once a annotation view is selected
_calloutView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, _titleLabel.frame.size.height + _subtitleLabel.frame.size.height + _infoButton.frame.size.height + 5)];
_calloutView.hidden = YES;
_calloutView.userInteractionEnabled = YES;
[self addSubview:_calloutView];
// create rounded rect background and size to text
UIImageView *backgroundRect = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, _calloutView.frame.size.width, _calloutView.frame.size.height)];
//[backgroundRect setAlpha:0.8];
//[backgroundRect setOpaque:NO];
[backgroundRect.image drawInRect:_calloutView.frame];
UIGraphicsBeginImageContext(backgroundRect.frame.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect frame = _calloutView.bounds;
[[UIColor colorWithRed:.2 green:.2 blue:.2 alpha:1] set];
CGPathRef roundedRectPath = [self roundedRectPath:frame radius:5];
CGContextAddPath(ctx, roundedRectPath);
CGContextFillPath(ctx);
CGPathRelease(roundedRectPath);
backgroundRect.userInteractionEnabled = YES;
backgroundRect.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[_calloutView addSubview:backgroundRect];
[_calloutView addSubview:_titleLabel];
[_calloutView addSubview:_subtitleLabel];
[_calloutView addSubview:_infoButton];
// position callout above pin
_calloutView.frame = CGRectMake(-140, -_calloutView.frame.size.height+10, _calloutView.frame.size.width, _calloutView.frame.size.height);
backgroundRect = nil;
return self;
【问题讨论】:
【参考方案1】:我的猜测是您的按钮将自身作为目标。如果按钮不是第一响应者并且被点击,则在上述场景中不会调用其目标函数。我通过使用惰性 var 在 Swift 4 Xcode 9 中声明 UIButton 来解决我的类似问题,将目标指向父视图/视图控制器。
// Sets target to parent view or view controller
// someFunction is called even if the button is not the first responder when tapped
lazy var nextCreateButton: UIButton =
let button = UIButton(type: .system)
button.addTarget(self, action: #selector(someFunction), for: .touchUpInside)
return button
()
// Sets target to the button itself
// someFunction is not called when the button is not the first responder
let nextCreateButton: UIButton =
let button = UIButton(type: .system)
button.addTarget(self, action: #selector(someFunction), for: .touchUpInside)
return button
()
【讨论】:
【参考方案2】:尝试使用[self addSubview:_calloutView]
在您将按钮和标签添加到 *_calloutView* 作为子视图,而不是之前。
告诉我结果如何
【讨论】:
感谢您的回答,但这并没有解决问题。 好吧,你能解释一下你的按钮没有响应吗?它是不可触摸的,你能在屏幕上看到它吗? 我可以在屏幕上看到它,但它是不可触摸的。所以我认为它隐藏在某些东西后面。以上是关于UIButton 不工作。不是第一响应者或隐藏在视图后面的主要内容,如果未能解决你的问题,请参考以下文章
Xcode:检查 UIButton 是不是隐藏,使用 NSUserDefaults