重新加载 UIView 和 UILabel 框架

Reload UIView and UILabel Frame

我在 nib 文件中有两个 UILabels 和一个 UIView。这个笔尖是我的 collectionView 的 header。在我的viewForSupplementaryElementOfKind:方法中,我需要改变这三个objects的帧。它们正在根据 header.personCollege 标签的大小进行更改。

[header.personCollege sizeToFit];
CGRect boxFrame = header.boxView.frame;
boxFrame.origin.x = header.personCollege.frame.origin.x + header.personCollege.frame.size.width + 11;
header.boxView.frame = boxFrame;

CGRect yearFrame = header.personYear.frame;
yearFrame.origin.x = boxFrame.origin.x + boxFrame.size.width + 11;
header.personYear.frame = yearFrame;

[header.personYear setNeedsDisplay];
[header.personCollege setNeedsDisplay];
[header.boxView setNeedsDisplay];

问题是setNeedsDisplay不换帧。帧仅在调用 reloadData 后更改。我需要立即更改框架。我也在使用自动布局。

由于您使用的是 AutoLayout,因此您应该更改 'constant' 与视图关联的约束值,而不是直接更改它们的框架。

您可以首先为要更改的约束创建 IBOutlets/IBOutletCollection,然后使用这些 IBoutlet 可以更改它们的 'constant' 值。