iOS - Find top constraint for a view?

前端 未结 7 1441
情深已故
情深已故 2020-12-28 16:52

I am trying to find the top constraint of the view in code. The top constraint is added in storyboard, and I don\'t want to use an IBOutlet.

Logging the value of the

7条回答
  •  不知归路
    2020-12-28 17:17

    I write a small extension in Swift:

    extension UIButton {
        var topConstraints: [NSLayoutConstraint]? {
            return self.constraints.filter( { ($0.firstItem as? UIButton == self && $0.firstAttribute == .top) || ($0.secondItem as? UIButton == self && $0.secondAttribute == .top) })
        }
    }
    

提交回复
热议问题