Center a UIImageView and a UILabel together in a U

2019-08-07 05:21发布

If I declare a UILabel and a UIImageView of different sizes can I then center them in a UIView with 10.0points between the two horizontally?

Thanks!

2条回答
姐就是有狂的资本
2楼-- · 2019-08-07 05:51

Yes you can. Basically what you need is appropiate constraints. You can add constraints from code or in stoaryboard

Code example:

NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeCenterX multiplier:1 constant:5];
[view addConstraint:constraint];

NSLayoutConstraint Documentation

Auto Layout Tutorial

Adaptive Layout Tutorial

查看更多
男人必须洒脱
3楼-- · 2019-08-07 05:59

This can be accomplished using AutoLayout. You cans set the right margin of the label to be 5 points to the left of the superview CenterX and the imageview left margin to be 5 points to the right of the superview CenterX.

查看更多
登录 后发表回答