gpt4 book ai didi

ios - 我如何才能在整个类(class)中访问约束,而不仅仅是在我设置约束的地方?

转载 作者:行者123 更新时间:2023-11-28 10:13:27 24 4
gpt4 key购买 nike

所以我的第一个想法是让它成为类的实例属性,然后在一个方法中设置它并在另一个方法中访问它。这并没有像我希望的那样真正有效,所以我想知道如何为约束设置标识符,以便我可以在类里面需要的任何地方访问它?我没有使用 Storyboard。所以我不能使用 socket 并在 GUI 中设置标识符。

iconOne.heightAnchor.constraint(equalToConstant: 25.0).isActive = true

我希望能够访问上述约束,以便稍后更新常量,最好的方法是什么?如果它不是标识符,请让我知道,因为我愿意接受任何建议。

最佳答案

iconOne.heightAnchor.constraint(equalToConstant: 25.0) 将返回一个 NSLayoutConstraint。将其存储在变量中。

class TestViewController: UIViewController {

var iconOne: UIImageView!

var heightConstraint: NSLayoutConstraint!

override func viewDidLoad() {
super.viewDidLoad()

heightConstraint = iconOne.heightAnchor.constraint(equalToConstant: 25.0)
heightConstraint.isActive = true

}

}

或者通过IB做一个outlet。

class TestViewController: UIViewController {

@IBOutlet weak var iconOne: UIImageView!
@IBOutlet weak var heightConstraint: NSLayoutConstraint!


}

关于ios - 我如何才能在整个类(class)中访问约束,而不仅仅是在我设置约束的地方?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44952347/

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