Create a button programmatically and set a background image

前端 未结 11 1133
自闭症患者
自闭症患者 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条回答
  •  孤街浪徒
    2020-12-07 18:00

    Try below:

    let image = UIImage(named: "ImageName.png") as UIImage
    var button   = UIButton.buttonWithType(UIButtonType.System) as UIButton
    button.frame = CGRectMake(100, 100, 100, 100)
    button .setBackgroundImage(image, forState: UIControlState.Normal)
    button.addTarget(self, action: "Action:", forControlEvents:UIControlEvents.TouchUpInside)
    menuView.addSubview(button)
    

    Let me know whether if it works or not?

提交回复
热议问题