Nic*_*ico 0 uibutton ios swift
我有一个我这样创建的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), 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
Run Code Online (Sandbox Code Playgroud)
问题是只有当你是远离中心的像素时才会触发敲击UIButton.我设置的图像是一个交叉的png X.
即使我轻拍十字架(但不完全在中间,感谢模拟器的精度),它也不起作用.
我不明白.背景不明确.我是否更改内容/图像边缘插入不会改变一件事.我是否使用图像渲染模式不会改变事物.
最佳做法是保持UI按钮的大小至少为44x44. https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/LayoutandAppearance.html
通过为每个交互元素提供足够的间距,让人们可以轻松地与内容和控件进行交互.给可触发控件一个大约44 x 44点的命中目标.
所以我会将你的框架设置为大于20pts,你的图像尺寸是多少20pts?可以在按钮外部剪裁,检查clipToBounds是否设置为yes.
还有一种更激进的方法,那就是创建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);
}
Run Code Online (Sandbox Code Playgroud)
另一件事是使用透视调查按钮(http://revealapp.com/)
| 归档时间: |
|
| 查看次数: |
1818 次 |
| 最近记录: |