ios - 自定义 UILabel、UIButton 类的 viewWillAppear 替代方案

标签 ios swift

UIIbutton 类有没有类似于 viewWillAppear() 的函数?
以下所有这些函数仅调用一次

prepareForInterfaceBuilder()

awakeFromNib()

init()

最佳答案

所以不完全像 viewWillAppear,不。

这是因为这些是架构的根本不同方面。一个通常是经理(一个viewController),另一个总是一个仆从(UIView)。
它们的生命周期自然不同。

但是如果你想在你的自定义 View 中执行更改,无论是 UIButton 还是任何其他 UIView 子类,你可以做的重置是使用方法 setNeedsLayout() .

来自苹果文档

Call this method on your application’s main thread when you want to adjust the layout of a view’s subviews. This method makes a note of the request and returns immediately. Because this method does not force an immediate update, but instead waits for the next update cycle, you can use it to invalidate the layout of multiple views before any of those views are updated. This behavior allows you to consolidate all of your layout updates to one update cycle, which is usually better for performance. Blockquote



您应该通过覆盖对 View 的设置执行进一步的操作layoutSubviews()方法

同样,来自文档

Subclasses can override this method as needed to perform more precise layout of their subviews. You should override this method only if the autoresizing and constraint-based behaviors of the subviews do not offer the behavior you want. You can use your implementation to set the frame rectangles of your subviews directly. You should not call this method directly. If you want to force a layout update, call the setNeedsLayout() method instead to do so prior to the next drawing update. If you want to update the layout of your views immediately, call the layoutIfNeeded() method.



希望我在解释中已经很清楚了。

您还可以使用代码发布您的确切情况,以获得更清晰和答案。

关于ios - 自定义 UILabel、UIButton 类的 viewWillAppear 替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59436070/

相关文章:

ios - Storyboard堆栈 View 未连接到 View 文件(Swift)

ios - 嵌套(for-)循环的奇怪性能问题

javascript - React-native-camera - 类型错误 : undefined is not an object

ios - Swift + Core Data 只保存最后一个循环。

swift - 从多个 UICollectionVIews 内部的 UICollectionViewCell 推送一个新的 UICollectionViewController

ios - 我可以通过 Google Analytics 跟踪特定用户 ID 的 session 持续时间吗?

ios - 如何从我的 iphone 为我的应用程序浏览 pdf 文件,就像我能够通过 ibooks 浏览 pdf 文件一样?

ios - 如果按得太久,快速长按将停止工作

swift - XCode 7 自动完成功能无法正常工作

arrays - 转换字符串?