简体   繁体   中英

UIStackView multiline buttons overlapping

I'm trying to dynamically add UIButtons to UIStackView , but came across some strange behavior. As the text inside button is dynamic too, it should be multiline (by default it's not). So I set break mode like this:

 button.titleLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping
 stackView.addArrangedSubview(button)

The result looks like this (I made buttons different background color to better illustrate the problem):

问题

Text exceeds button's borders and overlaps other views. How can this be fixed?

UIStackView is loaded from this simple xib:

我在IB的xib

Thanks in advance

The titleLable is not constrained to the bounds of the button, that's the problem. You should extend the UIButton and override either layoutSubviews or intrinsicContentSize or just try to inject constraints.

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