3

This is working great:

                let label = UILabel()
                label.text = VARIABLE_LENGTH_STRING
                stackView?.addArrangedSubview(label)

but this does not work

                let button = UIButton()
                button.setTitle(VARIABLE_LENGTH_STRING, for: .normal)
                stackView?.addArrangedSubview(button)

Nothing displays! any idea why? I'd expect labels to behave pretty simularly to buttons...

Thanks for your help!

Michael Economy
  • 608
  • 6
  • 21

3 Answers3

2

The problem is that by default - a new UIButton() has white text and a white background --- grrrr!

i fixed this by doing:

button.setTitleColor(.blue, for: .normal)
Michael Economy
  • 608
  • 6
  • 21
1

try by adding below line in your code after you are setting title of button,

 button.sizeToFit()
Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
0

First check is there any value in "VARIABLE_LENGTH_STRING" if it has empty string nothing will show and give button frame size so that if there is no text button height width will be specific and one more thing change buttons background color by defaults it is clear so u will only see content of button on screen if any by changing background color and frame u will be see it on screen event it has no content to show :) .

junaid
  • 193
  • 1
  • 16