在 UIView 中将 UIImageView 和 UILabel 居中

Center a UIImageView and a UILabel together in a UIView

如果我声明了一个不同大小的 UILabel 和一个 UIImageView,我可以将它们置于 UIView 的中心,两者之间的水平间距为 10.0points 吗?

谢谢!

这可以使用 AutoLayout 来完成。 您可以将标签的右边距设置为父视图 CenterX 左侧 5 磅,图像视图左边距设置为父视图 CenterX 右侧 5 磅。

是的,你可以。基本上你需要的是适当的约束。您可以从代码或在 stoaryboard

中添加约束

代码示例:

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