英文:
Frame and bounds not being updated while setting constraints
问题
在创建约束时,似乎受约束的 UI 元素的框架和边界没有立即更新。
NSLayoutConstraint.activate([
counterLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
counterLabel.topAnchor.constraint(equalTo: timeLabel.bottomAnchor, constant: view.frame.size.height/100),
counterLabel.widthAnchor.constraint(equalToConstant: view.frame.size.width/1.2),
gameButton.heightAnchor.constraint(equalToConstant: (view.bounds.size.height-counterLabel.frame.maxY)) // 这里不起作用
事实证明,尽管为 counterLabel 创建了约束,但其框架和边界仍为 (0,0,0,0)。我发现的解决方法是将 gameButton 的高度锚点放在 viewDidLayoutSubviews 中。
为什么会这样?我尝试查找其他关于此问题的线程和文档,但没有找到太多信息。
英文:
While creating constraints, it seems like the frame and bounds of a constrained UI element is not being updated immediately.
NSLayoutConstraint.activate([
counterLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
counterLabel.topAnchor.constraint(equalTo: timeLabel.bottomAnchor, constant: view.frame.size.height/100),
counterLabel.widthAnchor.constraint(equalToConstant: view.frame.size.width/1.2),
gameButton.heightAnchor.constraint(equalToConstant: (view.bounds.size.height-counterLabel.frame.maxY)) // THIS DOES NOT WORK
It turns out that despite creating the constraints for counterLabel, its frame and bounds are still (0,0,0,0). The solution that I found to work was putting the gameButton's height anchor within viewDidLayoutSubviews.
Why is this the case? I tried looking for other threads and documentation about this, but couldn't find much.
答案1
得分: 0
在使用 frame 或 bounds 之前,先调用 layoutIfNeeded(),例如:
layoutIfNeeded()
myView.layer.cornerRadius = myView.frame.width / 2
现在 frame 和 bounds 会返回正确的数值。
在你的情况下,也许你需要设置约束,然后计算它们,并将约束存储在变量中。
英文:
write layoutIfNeeded() before using frame or bounds
for example
layoutIfNeeded()
myView.layer.cornerRadius = myView.frame.width / 2
now frame and bounds returns true values
in your case, maybe you need to set constraints, then calculate them and store your constraints in variables.
答案2
得分: 0
不要回答我要翻译的问题。以下是要翻译的内容:
"Instead of trying to set the button's height as a constant based on values that can change over time, you should set the button's top and bottom anchors so its height will adjust accordingly.
Replace:
gameButton.heightAnchor.constraint(equalToConstant: (view.bounds.size.height-counterLabel.frame.maxY))
with something like:
gameButton.topAnchor.constraint(equalTo: counterLabel.bottomAnchor),
gameButton.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),"
英文:
Instead of trying to set the button's height as a constant based on values that can change over time, you should set the button's top and bottom anchors so its height will adjust accordingly.
Replace:
gameButton.heightAnchor.constraint(equalToConstant: (view.bounds.size.height-counterLabel.frame.maxY))
with something like:
gameButton.topAnchor.constraint(equalTo: counterLabel.bottomAnchor),
gameButton.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论