Le Mot Juiced
Le Mot Juiced

Reputation: 3849

Text does not appear on UIButton in swift

Pounding my head against this. Should be so simple.

Running in a playground inside MyClass:

func configureButton(){
    let btn: UIButton = UIButton(frame: CGRectMake(5, 75, 90, 20))
    btn.backgroundColor = UIColor.greenColor()
    btn.addTarget(self,
                  action: #selector(MyClass.buttonTapped),
                  forControlEvents: UIControlEvents.TouchUpInside)
    addSubview(btn)
    btn.setTitleColor(UIColor.blackColor(), forState: .Normal)
    btn.titleLabel?.font = UIFont(name: "Helvetica", size: 40)
    btn.titleLabel?.text = "tap me"
  }

The button shows up green, and works when pressed, but does not display the text.

Upvotes: 0

Views: 2318

Answers (1)

matt
matt

Reputation: 535231

This line is just wrong:

btn.titleLabel?.text = "tap me"

Do not attempt to manipulate the button's title label text directly like this. Always pass through the button's official title setter, setTitle:forState:.

Upvotes: 7

Related Questions