【发布时间】:2018-02-03 21:02:20
【问题描述】:
我有一个这样创建的 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
问题是只有当您距离UIButton 的中心一个像素时才会触发点击。我设置的图像是一个交叉的png X。
即使我点击十字(但不完全在中间,感谢模拟器的精确度),它也不起作用。
我不明白。背景不清楚。无论我是否更改内容/图像边缘插图都不会改变任何事情。无论我是否使用图像渲染模式都不会改变任何事情。
【问题讨论】:
-
您是否尝试手动设置按钮的框架?还是更改按钮类型?
-
closeButton.frame 打印出什么?
-
刚刚用框架更新了帖子
-
打印出
(x : 345, y : 10, width : 20, height : 20)