center-vfl icon indicating copy to clipboard operation
center-vfl copied to clipboard

Centering a view both in horizontal and vertical

Open VictorZhang2014 opened this issue 7 years ago • 3 comments

Sometimes, I think there is an another solution, I copied my solution here.

If you want to Horizontal Alignment, just use it

[parentView addConstraint:[NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:parentView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];

If you want to Vertical Alignment , just use it

[parentView addConstraint:[NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:parentView attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];

And this solution works for me, I hope that I share it to someone else.

VictorZhang2014 avatar Mar 14 '17 03:03 VictorZhang2014

Yes, your method is actually simpler. Thanks for sharing @VictorZhang2014.

evgenyneu avatar Mar 14 '17 03:03 evgenyneu

We make progress together. @evgenyneu

VictorZhang2014 avatar Mar 14 '17 04:03 VictorZhang2014

Yes 🚀 😄

evgenyneu avatar Mar 14 '17 04:03 evgenyneu