gpt4 book ai didi

ios - 最好在约束更改之前或之后调用 .layoutIfNeeded()

转载 作者:行者123 更新时间:2023-11-29 05:10:50 28 4
gpt4 key购买 nike

我有一个按钮,可以根据某些情况改变大小。我不需要完成动画,因为用户永远看不到发生的变化。有时,当从较小尺寸切换回较大尺寸时,按钮会卡在较小尺寸上。我认为 layoutIfNeeded() 可以解决这个问题。

问题是我应该在什么时候调用下面的 setCameraButtonToNormalSize() 函数中的 layoutIfNeeded()

lazy var cameraButton: UIButton = {
let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false
button.addTarget(self, action: #selector(cameraButtonPressed), for: .touchUpInside)
return button
}()

let normalSize: CGFloat = 50
let smallerSize: CGFloat = 5

var cameraButtonWidthConstraint: NSLayoutConstraint?
var cameraButtonHeightConstraint: NSLayoutConstraint?

func setCameraButtonToNormalSize() {

// before the constraints are changed

cameraButtonWidthConstraint?.isActive = false
cameraButtonHeightConstraint?.isActive = false

cameraButtonWidthConstraint = cameraButton.widthAnchor.constraint(equalToConstant: normalSize)
cameraButtonWidthConstraint?.isActive = true
cameraButtonHeightConstraint = cameraButton.heightAnchor.constraint(equalToConstant: normalSize)
cameraButtonHeightConstraint?.isActive = true

// after the constraints are changed
}

fileprivate func setCameraButtonToSmallerSize() {

cameraButtonWidthConstraint?.isActive = false
cameraButtonHeightConstraint?.isActive = false

cameraButtonWidthConstraint = cameraButton.widthAnchor.constraint(equalToConstant: smallerSize)
cameraButtonWidthConstraint?.isActive = true
cameraButtonHeightConstraint = cameraButton.heightAnchor.constraint(equalToConstant: smallerSize)
cameraButtonHeightConstraint?.isActive = true
}

最佳答案

您需要在之后调用它

// after the constraints are changed
self.view.layoutIfNeeded()

关于ios - 最好在约束更改之前或之后调用 .layoutIfNeeded(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59696366/

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