Auto Layout (Constraints) Center 2 side by side views in a parent view

前端 未结 8 1350
小蘑菇
小蘑菇 2020-12-12 12:35

I\'m trying to figure out how to do this with auto layout (iOS6) and constraints.

Basically I have my large view divided up into two sections on the bottom. Inside o

8条回答
  •  猫巷女王i
    2020-12-12 13:20

    I figured out a way without adding another view:

     [aView addConstraint:[NSLayoutConstraint constraintWithItem:viewOnLeft attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationLessThanOrEqual toItem:aView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
     [aView addConstraint:[NSLayoutConstraint constraintWithItem:viewOnRight attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationLessThanOrEqual toItem:aView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
    

    You can also change the constants to create a gap between the views.

    • left view constraint constant: -X
    • right view constraint constant: +X

    centering subviews

提交回复
热议问题