Semyon Danilov
Semyon Danilov

Reputation: 1773

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):

The Problem

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

UIStackView is loaded from this simple xib:

My xib in IB

Thanks in advance

Upvotes: 0

Views: 853

Answers (1)

Simon
Simon

Reputation: 66

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.

Upvotes: 1

Related Questions