Search code examples
swiftautolayoutxcode10

iOS: Problem with horizontal centers alignment and layout


I want the below Yes and No buttons to be at bottom center of the screen side-by-side, always with same width. The other labels are behaving as expected. Problem is with the buttons. They just appear with skewed size with different widths, not centered to the bottom of the UIView.

I tried many ways - aligned all the elements centrally horizontal etc. Also I embedded these button in a UIView and aligned the header label, description label and this UIView centrally. Did not work.

I did not try code as I was trying in Xcode Storyboards.

Here is how I want... enter image description here


Solution

  • You can achieve it by that steps:
    1. Embed buttons into a view
    2. Add a width constrain between buttons.
    3. Add left, top, bottom insets to the YES button.
    4. Add right, top, bottom insets to the NO button.
    5. Add constraint to equal width of the YES and NO buttons.
    6. Add bottom inset constraint for the view.
    7. Add add center horizontally constraint to the view.
    8. If buttons have less width when you need, add width constraint to the YES button.

    Screenshot of constraints for the buttons

    Screenshot of constraints for the view