Getting an existing NSLayoutConstraint for the width?

前端 未结 3 743
自闭症患者
自闭症患者 2021-02-02 14:40

I\'m trying to animate a control in Cocoa with auto layout.

Now, I can set [[constraint animator] setConstant:newWidth];, which works. But how can I get the

3条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-02 14:49

    I wrote a reusable extension: (Swift 4.1)

    /**
     * Utils when working with constraints
     */
    extension NSLayoutConstraint{
        /**
         * Returns all constraints of kinds
         * EXAMPLE: NSLayoutConstraint.ofKind(rect.immediateConstraints, kinds: [.width,.height]) //width, height
         */
        static func ofKind(_ constraints:[NSLayoutConstraint],kinds:[NSLayoutAttribute]) -> [NSLayoutConstraint]{
            return kinds.map { kind in
                return constraints.filter { constraint in
                    return constraint.firstAttribute == kind
                }
            }.flatMap({$0})//flattens 2d array to 1d array
        }
    }
    

提交回复
热议问题