IOS 自动布局: 两个宽度相等的按钮,并排放置

我目前在自动布局方面有困难。我正在使用 Interface Builder,并试图定位两个等宽按钮并排如下图所示。

objective layout

从下面的预览图像,我的 titleImage 已被正确约束,并正在正确显示,但按钮没有。我已经尝试过将按钮1与 titleImage 的前沿对齐,将按钮2与 titleImage 的后沿对齐,但是这两个按钮之间的宽度分布在这一点上会出现偏差,如下所示。

problem scenario

我的目标是了解哪些约束是缺失的,应该适用于两个按钮,以保持相同的宽度,无论设备。如果可能的话,我希望通过 Interface Builder 而不是额外的代码来实现这一点。

59753 次浏览

Add the following constraints

  1. Assign equal width from button1 to button2.
  2. Assign horizontal spacing between both buttons.
  3. Assign leading space from button1 to its superview.
  4. Assign trailing space from button2 to its superview.
  5. Assign top space to both the buttons.
    Let me know if it works for you.

You can check my example - you can easy aim this, by using proportional constraint. Also you can easy aim proportional multiple UIViews. Please look attached example

https://dl.dropboxusercontent.com/u/48223929/TestViewController.xib

Stack layout in iOS9, will do the job really nice. Add stack view to your view and configure as follow:

enter image description here

My solution is to

  1. Put a small view in the middle of two buttons and make it centre(Horizontal centre in container and vertical centre in contener as 0).
  2. Add height and width to the small view.
  3. Add buttons the constraints and give horizontal space constraints to small view.
  4. Give the small view background colour same as buttons or View's colour.

Note: See the Screenshot.

enter image description here

Follow Steps and Screenshots for easy solution


Step-1)

  • For Button 1 : Set Constraints: (1)Leading, (2)Top or Bottom asper your need, (3)Height


Step-2)

  • For Button 2 : Set Constraints: (1)Trailing,(2)Top or Bottom asper your need, (3)Height

Step-3)

  • Press Ctrl + Drag From Button 1 to Button 2

  • Select Horizontal Spacing


Step-4)

  • Select Both Button (using Command) and Add Constraints Equal Width


OUTPUT

Hope it Helps you :)