【发布时间】:2015-02-14 16:52:15
【问题描述】:
我在这里找到了很多解决方案,但不适用于 Swift,而且我知道您可以使用 UIImageView 做到这一点,但在我的情况下,我需要为 以编程方式设置 alpha 透明背景图像用户界面按钮。最好是 UIImage 扩展!
let img = UIImage(named: "imageWithoutAlpha")
var imgInsets = UIEdgeInsetsMake(0, 24, 0, 24)
image = image!.resizableImageWithCapInsets(imgInsets)
let myButton = UIButton(frame: CGRect(x: 50, y: 50, width: img!.size.width, height: img!.size.height))
myButton.setBackgroundImage(img, forState: UIControlState.Normal)
myButton.contentEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 20)
myButton.setTitle("Startbutton", forState: UIControlState.Normal)
myButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
myButton.sizeToFit()
view.addSubview(myButton)
当前结果:
想要的结果:
【问题讨论】:
标签: swift uibutton uiimage uicontrolstate