ios - 重新加载 UIView 和 UILabel 框架

标签 ios uiview autolayout setneedsdisplay

我在一个 nib 文件中有两个 UILabels 和一个 UIView。这个 nib 是我的 collectionView 的 header 。在我的 viewForSupplementaryElementOfKind: 方法中,我需要更改这三个对象的框架。它们根据 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,因此您应该更改与 View 关联的约束的“常量”值,而不是直接更改它们的框架。

您可以首先为要更改的约束创建一个 IBOutlets/IBOutletCollection,然后使用这些 IBoutlets 可以更改它们的“常量”值。

关于ios - 重新加载 UIView 和 UILabel 框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31064522/

相关文章:

ios - 如何根据屏幕尺寸更改字体大小

iphone - iOS 应用程序无法在 iOS 6 或更低版本上运行

ios - - 使用 `$(inherited)` 标志,或 - 从目标中删除build设置。 CocoaPod Swift3 pod更新报错

iphone - 在另一个 View 上显示 UIViewController/UIView

另一个 View 范围内的 iOS View

objective-c - 添加到 View 的 subview 的 UIButton 不会响应

uitableview - UIStackview 在 UITableviewCell 中按比例填充采用不正确的框架

ios - 如何快速在 IQKeyboardManagerSwift 上添加日期选择器 View

ios - cancelPreviousPerformRequests 似乎在 Swift 3.0 中不起作用

ios - 应用程序在后台运行时如何处理 AVPlayer 错误?