What is the best approach for horizontally aligning 5 buttons with Autolayout

后端 未结 3 1692
余生分开走
余生分开走 2021-01-15 01:13

I know this is a little newbie style question but I couldn\'t find any subject about this and I have to horizontally align multiple buttons in my view controller.

I

3条回答
  •  温柔的废话
    2021-01-15 01:50

    You just need to give leading, trailing and equal-widths constraints between adjacent buttons.

    Steps (We'll only consider horizontal constraints here):

    1. Add leading constraint from the first button to the superview (or left-side sibling view).
    2. Add horizontal spacing constraint between:
      • First button and second button.
      • Second button and third button.
      • Third button and fourth button.
      • Fourth button and fifth button.
    3. Add trailing constraint from the fifth button to the superview (or right-side sibling view).
    4. Add equal widths constraint between:
      • First button and second button.
      • Second button and third button.
      • Third button and fourth button.
      • Fourth button and fifth button.

    If you follow this approach, you need not add extra views to align the buttons. Please see the screenshots below for understanding this better.

提交回复
热议问题