Ios 带有透明背景图像的UIButton无法正常工作

Ios 带有透明背景图像的UIButton无法正常工作,ios,swift,uibutton,Ios,Swift,Uibutton,我用透明的PNG创建了UIButton instantHelpBtn = UIButton() instantHelpBtn.frame = CGRectMake(10, 10, 30, 30) instantHelpBtn.setBackgroundImage(UIImage(named: "questions1.png"), forState: .Normal) instantHelpBtn.addTarget(self, action: "instantHelpBtnPressed

我用透明的PNG创建了UIButton

 instantHelpBtn = UIButton()
 instantHelpBtn.frame = CGRectMake(10, 10, 30, 30)
 instantHelpBtn.setBackgroundImage(UIImage(named: "questions1.png"), forState: .Normal)
 instantHelpBtn.addTarget(self, action: "instantHelpBtnPressed", forControlEvents: .TouchUpInside)
 self.view.addSubview(instantHelpBtn)
instantHelpBtn.backgroundColor = UIColor.clearColor() 

问题是,只有在我以某种方式按下背景图像的不透明部分时,函数
instantThelpbtnpressed
才起作用。如果我错过了(而且我经常错过),什么都不会发生。如何更改此行为?

与其将其设置为透明图像,不如

instantHelpBtn.backgroundColor = UIColor.clearColor() 
如果您想在单击“执行”时将其设置为图像

instantHelpBtn.backgroundColor = UIColor.clearColor() 
func instantHelpBtnPressed(sender: UIButton) {
   sender.setBackgroundImage(UIImage(named: "questions1.png"), forState: .Normal)
}

问题是你的按钮框。您正在创建
30X30
UIButton
其中,根据Apple iOS人机界面指南,最小尺寸应为
44X44

,在我看来,30*30帧太小了。苹果建议创建不小于44x44像素的ui可点击元素。这里有一个关于文档的链接:

增加您的按钮框,问题就解决了,否则减少您的图像框问题就解决了。很高兴为您提供帮助。。。干杯:)!
instantHelpBtn.backgroundColor = UIColor.clearColor()