Centering two buttons with Cocoa Auto Layout

后端 未结 7 1622
抹茶落季
抹茶落季 2021-02-03 22:13

I have a problem with Cocoa Auto Layout and can\'t get my head around this problem. All I\'d like to achieve is to have two buttons always centered in a view as depicted below.<

7条回答
  •  情书的邮戳
    2021-02-03 23:16

    A neat trick with Auto Layout is to use invisible views as spacers. The constraint system still lays them out as normal. In this case, that space between the two buttons can be an invisible view. You can use the constraints from this constraint string:

    @"[button][invisibleView(5)][button2(==button)]"
    

    Then create a constraint setting invisibleView.centerX = superview.centerX.

提交回复
热议问题