Create a button programmatically and set a background image

前端 未结 11 1096
自闭症患者
自闭症患者 2020-12-07 17:44

When I try creating a button and setting a background image in Swift:

let button   = UIButton.buttonWithType(UIButtonType.System) as UIButton
    button.fram         


        
11条回答
  •  -上瘾入骨i
    2020-12-07 17:59

    SWIFT 3 Version of Alex Reynolds' Answer

    let image = UIImage(named: "name") as UIImage?
    let button = UIButton(type: UIButtonType.custom) as UIButton
    button.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
    button.setImage(image, for: .normal)
    button.addTarget(self, action: Selector("btnTouched:"), for:.touchUpInside)
            self.view.addSubview(button)
    

提交回复
热议问题