Autolayout & programmatic constraints: How to deal with updateConstraints firing multiple times?

后端 未结 2 2119
太阳男子
太阳男子 2020-12-28 17:09

When programmatically creating layouts, I follow Apple\'s advice: override -updateConstraints, add custom constraints, and call -setNeedsUpdateConstraints once subviews have

2条回答
  •  清酒与你
    2020-12-28 17:39

    This kind of tracking could be done too, for the initial setup. In most cases.

    override func updateConstraints() {
                if constraints.count == 0 {
                    let views = ["textField": textField]
                    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[textField]-0-|", options: [], metrics: nil, views: views))
                    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[textField]-0-|", options: [], metrics: nil, views: views))
                }
                super.updateConstraints()
            }
    

提交回复
热议问题