Get reference to NSLayoutConstraint using Identifier set in storyboard

前端 未结 6 1522
旧时难觅i
旧时难觅i 2020-12-30 02:02

I was setting the constraints of a button using a storyboard. I saw an option, \"Identifier\" in the constraint\'s properties.

6条回答
  •  无人及你
    2020-12-30 02:40

    For resizing a set of buttons within a common view container this works. Each subview/button must use a common identifier (e.g. "height").

    @IBAction func btnPressed(_ sender: UIButton) {
        for button in self.btnView.subviews{
            for constraint in button.constraints{
                if constraint.identifier == "height"{
                    constraint.constant = constraint.constant == 0 ? 30:0
                }
            }
        }
        UIView.animate(withDuration: 0.3) { () -> Void in
            self.view.layoutIfNeeded()
        }
    }
    

提交回复
热议问题