【问题标题】:Make title part of clickable area in UIButton在 UIButton 中使标题成为可点击区域的一部分
【发布时间】:2019-12-19 10:38:06
【问题描述】:

我有一个带有图像和长标题的 UIButton。我调整了按钮的 titleEdgeInsets,使文本出现在按钮图像的右侧。

    button.titleEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, 0.0, -BUTTONSIZE - button.titleLabel.frame.size.width - 15.0);

问题是按钮只有图像是可点击的,而不是它右侧的长标题,我想让它也触发点击事件。有没有一种简单的方法可以做到这一点,还是我必须添加某种叠加视图?

谢谢

【问题讨论】:

  • 请分享您的代码sn-p,它会让更多人理解。
  • 我的猜测是您的按钮标题超出了按钮框架。在这种情况下,您必须使按钮变大。
  • @LGP 是正确的。我不得不扩展按钮的框架,以及在按钮上使用 setImage 而不是 setBackgroundImage

标签: ios objective-c uibutton


【解决方案1】:

获取触摸点,检查是否在图片范围内:

- (IBAction)myAction:(UIButton *)sender forEvent:(UIEvent *)event {
    NSSet *touches = [event touchesForView:sender];
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:sender];
    NSLog(@"%@", NSStringFromCGPoint(touchPoint));
    if ( CGRectContainsPoint(imageFrame, touchPoint) ) {
        // Point lies inside the bounds.
    }
}

【讨论】:

    猜你喜欢
    • 2010-10-22
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-15
    • 2023-03-23
    • 1970-01-01
    • 2010-10-31
    相关资源
    最近更新 更多