首页 > 解决方案 > 在 UIButton 中使标题成为可点击区域的一部分

问题描述

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

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

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

谢谢

标签: iosobjective-cuibutton

解决方案


获取触摸点并检查它是否在图像范围内:

- (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.
    }
}

推荐阅读