Search code examples
iosswiftxcodeautolayoutconstraints

How can I make object auto resize in xcode auto layout?


I can't make the Date Picker automatically resize its width. I want the Horizontal constraints showed into the attached images, that is

for DATE Label: Leading Space to Superview = 20 Trailing Space to Date Picker = 20

for Date Picker: Leading Space to DATE Label = 20 Trailing Space to Superview = 20

Xcode says that Width and horizontal position are ambiguous for "Date Picker". But I don't want a fixed size for the Date Picker, I want it resizes itself based on the assigned constraints.

Please help, Thank you!

Date Picker constraints Label constraints


Solution

  • Auto-layout needs to know which element should get priority for horizontal sizing.

    Select your label, and change Horizontal Compression Resistance Priority to Required:

    enter image description here