【发布时间】:2012-09-28 17:11:27
【问题描述】:
我有一个UILabel 我有时想在单元格中垂直居中。这必须以编程方式完成。我尝试添加以下约束以使其居中(locName 是 UILabel):
[cell addConstraint:[NSLayoutConstraint constraintWithItem:cell
attribute:NSLayoutAttributeCenterY relatedBy:0 toItem:cell.locName
attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
这是我得到的结果:
它看起来像它的中心,但可能会改变 UILabel 的高度或其他什么?
这是 IB 中的视图控制器:
如何以编程方式实现垂直居中?
【问题讨论】:
-
为什么必须以编程方式完成?为什么不能简单地使用 IB 设计一个不同的单元并隔离它而不是这个?
-
谢谢!这将解决我的具体情况。如果我们不处理细胞,你会怎么做?假设我们只是试图以编程方式将标签居中在通用 UIView 中?
-
这可能不是最好的方法,但我可能会计算高度并做一些数学运算来确定框架的 CGRect 应该是什么。
标签: iphone ios ios6 centering autolayout