Noja Noja - 7 months ago 29
Swift Question

Programmatically Created Square Error

i am creating a square programatically though when i run the app, nothing shows up.

var PB1: UIButton!

func SetUpButton1() {

var PB1: UIButton!
PB1.setBackgroundImage(UIImage(named: "RS"), forState: UIControlState.Normal)
PB1.center = CGPoint(x: 200, y: 200)
PB1.bounds = CGRect(x: 0, y: 0, width: 300, height: 50)

view.addSubview(PB1)

}

Answer

you need to intialize PB1 object with UIButton()

  let PB1 = UIButton()
  PB1.bounds = CGRect(x: 0, y: 0, width: 300, height: 50)

  PB1.setBackgroundImage(UIImage(named: "RS"), forState: UIControlState.Normal)
  PB1.center = CGPoint(x: 200, y: 200)

  self.view.addSubview(PB1)
Comments