Search code examples
iosswiftuiviewuistoryboard

Creating a UIView with multiple items


I am trying to create a view where I collect user information like first name, last name, DoB among others. The view when created in the storyboard looks like this -- enter image description here

However, setting constraints on each of these is turning out to be really unwieldy. I am very much inclined to creating this in code, and ofcourse it feels like too many lines even in code. There has to be a better way to do it.

What is the best way to do it? Do I put them in multiple stack views and then stack them up in additional stack views? What is the usual standard practice?

I am very new to iOS programming and searching on stackoverflow and google did not give me any results.


Solution

  • Use always stack view if you can, they are the best solution because they manage many issues for you. In this situation I will put:

    • First name Label and first name txt in a vertical stack view (A)
    • Last name Label and Last Name Txt in a vertical stack view (B)
    • Cup, Cupertino, Cup Txt in a horizontal stack view (C)
    • DoB Label in a horizontal stack view (even if is only a button is better put it in a stack view to help you to center the button (D)
    • The button Done in a horizontal stack view (even if is only a button is better put it in a stack view to help you to center the button (E)
    • A,B,D,C,E in a vertical stack view

    Now it can be messy, but don't worry. You need to set the constraints and the stack view parameters to adjust. Try the different options, the only way to understand how to set up a correct stack view is to try. After sometime you'll able to create a complex interface in a few minutes.

    For a better understanding see the documentation: https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/LayoutUsingStackViews.html

    And watch this video from Apple:Autolayout Apple Video