简体   繁体   中英

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!

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)

尝试在设置按钮标题后在代码中添加以下行,

 button.sizeToFit()

首先检查“VARIABLE_LENGTH_STRING”中是否有任何值,如果它有空字符串,则不会显示任何内容并给出按钮框架大小,这样如果没有文本按钮,高度宽度将是特定的,并且默认情况下更改按钮背景颜色还有一个很明显所以你只会通过改变背景颜色和框架看到屏幕上的按钮内容你将在屏幕上看到它没有内容显示的事件:)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM