ios - 在 UIButton 中使标题成为可点击区域的一部分
问题描述
我有一个带有图像和长标题的 UIButton。我调整了按钮的 titleEdgeInsets,使文本出现在按钮图像的右侧。
button.titleEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, 0.0, -BUTTONSIZE - button.titleLabel.frame.size.width - 15.0);
问题只是按钮的图像是可点击的,而不是它右侧的长标题,我也想让它触发点击事件。有没有一种简单的方法可以做到这一点,还是我必须添加某种覆盖视图?
谢谢
解决方案
获取触摸点并检查它是否在图像范围内:
- (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.
}
}
推荐阅读
- excel - 如何查找多个数字的单元格并在 Excel 中更改其值
- c# - 自定义何时在 NLog 中捕获堆栈跟踪
- django - Django OAuth 工具包登录模板
- ios - 如何使用内部 XIB 为动态 UIStackView 设置对齐方式
- python - RuntimeWarning:在 double_scalars numpy 中遇到溢出
- bcc-compiler - BCC LD86 文件的幻数错误
- oracle - ORA-01841:(完整的)年份必须......不是 0。如何重现和修复?
- c# - SQL Server 存储过程的 JSON 字符串输出参数在 SSMS 中有效,但在 C# 客户端中产生空值
- excel - 在具有多项选择的 Excel 表中查找索引/值
- javascript - 黑莓 spark SDK 身份配置