Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert UIButtons of Dynamic size into a UIStackView dynamically?

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!

like image 687
Michael Economy Avatar asked Jan 31 '26 04:01

Michael Economy


1 Answers

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)
like image 90
Michael Economy Avatar answered Feb 02 '26 21:02

Michael Economy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!