Get reference to NSLayoutConstraint using Identifier set in storyboard

前端 未结 6 1523
旧时难觅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:38

    Swift 3

    I wrote a quick NSView extension which handles this nicely.

    extension NSView {
        func constraint(withIdentifier: String) -> NSLayoutConstraint? {
            return self.constraints.filter { $0.identifier == withIdentifier }.first
        }
    }
    

    Usage:

    if let c = button.constraint(withIdentifier: "my-button-width") {
        // do stuff with c
    }
    

提交回复
热议问题