我创建了一个UIVut,其子视图类型为UIButton。按钮的大小比视图大。每当我触摸按钮时,只有与superview大小相同的部分才会响应触摸事件。我创建了一个视图层次结构图,以显示我的意思:
只有按钮的红色部分才能响应触摸事件。这是怎么回事?我该如何解决这个问题?
这是viewDidLoad中的代码,我以编程方式创建按钮
let button = UIButton(frame: CGRectMake(-8, -8, 52, 52))
moveView.addSubview(button)
button.setBackgroundImage(UIImage(named: "trIcon"), forState: UIControlState.Normal)
button.adjustsImageWhenHighlighted = false
button.addTarget(self, action: "gogo", forControlEvents: UIControlEvents.TouchUpInside)
moveView.clipsToBounds = false
UIView是在故事板中创建的。
答案 0 :(得分:1)
请在我尝试编码时按照说明进行操作
let button = UIButton(frame: CGRectMake(0, 0, giveValueLessthanMoveViewWidthSize, giveValueLessthanMoveViewHeightSize)) //Whatever give value less than your view size(x,y,width,height)
button.setBackgroundImage(UIImage(named: "trIcon"), forState: UIControlState.Normal)
button.adjustsImageWhenHighlighted = false
button.backgroundColor = UIColor.blueColor()
button.addTarget(self, action: "gogo:", forControlEvents: UIControlEvents.TouchUpInside)
moveView.clipsToBounds = false
moveView.addSubview(button)
然后按钮操作方法
func gogo(sender:UIButton!)
{
println("Button Clicked")
}