COOKIES
COOKIES

Reputation: 444

Minimum width for view when label is controlling width

I’ve been trying to do the following for quite some time without success.. I’m super grateful for any advice or tips.

Imagine having a UIView that has a fixed height and that’s horizontal/vertical in whatever superview it’s implemented in. Imagine that the view has a UILabel that controls its width.

How can you now set a minimum width for that UIView? Just in case the UILabel is empty or has a 2 character string, I’d like the UIView to have at least 50 pix width.

Here's a visual example:

Visual example

Imagine the UILabel having no text. Then the UIView would have 0 width. That's the problem I'm trying to solve.

Upvotes: 5

Views: 3803

Answers (2)

Christoph
Christoph

Reputation: 722

Create a width constraint with the minimum required width for the UIView and change the relation from Equal to Greater Than or Equal. enter image description here

Upvotes: 11

trapper
trapper

Reputation: 11993

Make the UIView.width = UILabel.width constraint priority less than 1000.

Add a UIView.width >= 50 constraint priority 1000.

Upvotes: 2

Related Questions