gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:35:50 26 4
gpt4 key购买 nike

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com