Search code examples
iosxcodestoryboard

Dynamic width view with subviews


I am trying to create the following in a xib:

https://i.sstatic.net/KIdu6.jpg

Please ignore the poor drawing. Basically, the black border is the main view. Inside that main view, I have a smaller view (in blue) that should be on the right side of the main view, centered vertically. The blue view also has a label as a subview, so I need the blue view to have a dynamic width based on the length of the label.

However, I can't seem to get the constraints right. Every time, I get red constraints and errors. What would the correct constraints for this be?


Solution

  • You need to give below three constraints

    • Vertically center to it superview

    • Constant trailing space.

    • Leading constrain with >= value to specify the value of x

    Storyboard constraints:

    Storyboard constraints

    Download demo code