Centering two buttons with Cocoa Auto Layout

后端 未结 7 1630
抹茶落季
抹茶落季 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条回答
  •  梦毁少年i
    2021-02-03 23:12

    You can achieved as below way also.

    1.Take Leading space for left button, Trailing space for right button.
    2.Construct Outlets for both Leading and Trailing constraints.

     __weak IBOutlet NSLayoutConstraint *leadingConstraint;
     __weak IBOutlet NSLayoutConstraint *trailingConstraint;
    

    3.Caluclate constants as below formula.

    NSInteger constant = (SCREEN_WIDTH - (CGRectGetWidth(leftButton.frame) + CGRectGetWidth(rightButton.frame))) / 3;
        leadingConstraint.constant = constant;
        trailingConstraint.constant = constant;
    

    Hope it will helps you.

提交回复
热议问题