gpt4 book ai didi

ios - UI 更新在不同情况下表现不同

转载 作者:行者123 更新时间:2023-11-28 08:38:42 25 4
gpt4 key购买 nike

场景 - 我的 View 上有标签,其高度和宽度我的方面适合 superView 的高度和宽度(这表示它们的尺寸是可变的,并根据 View 而变化。)。 我现在必须更改标签的属性。我在函数中这样做 -

func ammendViews()
{
noOfStudentsLbl.layer.masksToBounds = true
noOfStudentsLbl.layer.cornerRadius = noOfStudentsLbl.frame.height/2
maleCountLbl.clipsToBounds = true
maleCountLbl.layer.cornerRadius = maleCountLbl.frame.height/2
femaleCountLbl.clipsToBounds = true
femaleCountLbl.layer.cornerRadius = femaleCountLbl.frame.height/2

noOfStudentsLbl.hidden = false
maleCountLbl.hidden = false
femaleCountLbl.hidden = false


}

效果 1 - enter image description here

效果 2

enter image description here

案例 - 1(viewDidLoad() & viewWillAppear())

我在 viewDidLoad() 和 viewWillAppear() 中调用了这个函数,它产生了 effect1 。

案例 - 2(viewDidLayoutSubViews())

我在 viewDidLayoutSubViews() 中调用了它,更改最初显示为 effect1,一段时间后更改为 effect2。

案例 - 2(dispatch_async)

然后我在异步方法中放入一个 mainQueue 作为 -

override func viewWillAppear(animated: Bool)
{
dispatch_async(dispatch_get_main_queue()) {
self.ammendViews()
}

}

这显示了 effect-2 !!

我的理解-由于我的标签尺寸是动态的,并且取决于主视图的尺寸,即设备尺寸,并且当调用 viewWillAppear() 和 viewDidLoad() 时, View 没有正确放置,因此对我的标签只有一半的影响。

在调用 viewDidLayoutSubViews() 时, View 已经放置好,因此它进行了更改,但为什么它在中断几秒钟后生效,我不知道。

为什么当我把它放进去时它运行得非常完美

dispatch_async(dispatch_get_main_queue()) {
}

这里我是异步的,但是是在主线程中做的,那么为什么前面3个案例我直接在mainQueue中做(不是异步的)没有发生。

最佳答案

你的理解是正确的。 View 尚未在 viewDidLoadviewWillAppear 中布局。通过将您的代码放在 viewDidLayoutSubviews 中,自动布局已运行并且 View 布局正确,因此框架现在已正确设置。这是执行此操作的正确位置。

在使用 dispatch_async 的情况下,您正在安排该代码稍后在主线程上运行。碰巧的是,当 iOS 开始运行这段代码时, subview 已经布置好了。如果您将 print 放入您的异步代码和 viewDidLayoutSubviews 中,我相信您会发现 viewDidLayoutSubviews 将在您的异步代码运行之前完成。

关于ios - UI 更新在不同情况下表现不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36914976/

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