Ios 轻触UIButton小得可笑的区域
我创建了一个UIButton,如下所示:Ios 轻触UIButton小得可笑的区域,ios,swift,uibutton,Ios,Swift,Uibutton,我创建了一个UIButton,如下所示: let closeButton = UIButton(type: .System) closeButton.backgroundColor = UIColor(r: 92, g: 92, b: 118, alpha: 1) closeButton.setImage(UIImage(named: "closeCross")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate), forSt
let closeButton = UIButton(type: .System)
closeButton.backgroundColor = UIColor(r: 92, g: 92, b: 118, alpha: 1)
closeButton.setImage(UIImage(named: "closeCross")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate), forState: .Normal)
closeButton.tintColor = UIColor.whiteColor()
closeButton.imageEdgeInsets = UIEdgeInsets(top: -10, left: -10, bottom: -10, right: -10)
closeButton.addTarget(self, action: "close", forControlEvents: .TouchUpInside)
closeButton.frame = CGRectMake(messageView.frame.maxX - 30, 0, 20, 20)
closeButton.center.y = messageView.bounds.midY
问题是,只有当您距离ui按钮的中心有一个像素时才会触发点击。我设置的图像是一个png,它是一个交叉X
即使我点击十字架(但不完全在中间,感谢模拟器的精确性),它也不起作用
我不明白。背景不清楚。无论我是否更改内容/图像边缘插入,都不会改变任何东西。无论我是否使用图像渲染模式,都不会改变任何事情 下面是我如何修复它的:
closeButton.imageEdgeInsets = UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)
closeButton.frame = CGRectMake(noConnectionMessageView.frame.maxX - 40, 0, 40, 40)
它的最佳实践是保持UIbuttons的大小至少为44x44
通过给每个交互元素留出足够的间距,使人们更容易与内容和控件交互。给tappable控件一个大约44 x 44点的命中目标
所以我会把你的相框设置得比20分大,你的图像20分是多大?如果是在按钮外剪裁,请检查clipToBounds是否设置为“是”
还有一种更激进的方法,那就是创建一个UIButton的子类,其触摸面积至少为44
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
CGFloat widthDelta = 44.0 - self.bounds.size.width;
CGFloat heightDelta = 44.0 - self.bounds.size.width;
CGRect largerBounds = CGRectInset(self.bounds, -0.5 * widthDelta, -0.5 * heightDelta);
return CGRectContainsPoint(largerBounds, point);
}
另一件事是使用reveal()检查按钮。只有当UIButton上没有标题和图像时,才会出现这种情况。如果你不想添加一个标题,有一个黑客
只需添加一个空格作为标题文本。按钮的整个区域都会开始响应
它奇迹般地为我工作 您是否尝试手动设置按钮的边框?或者更改按钮类型?closeButton.frame打印出什么?刚刚用打印出的框架更新了帖子(x:345,y:10,宽度:20,高度:20)
比我快。负片的插入意味着图像实际上看起来比目标区域大。你不需要显示,它包含在新的XCode版本中。尝试调试面板旁边的按钮:大约到Xcode 12时,他们可能已经完成了该功能=P我发现它远不如揭示有用,但这是我个人的观点。同样值得注意的是,你必须为“揭示”付费。