How to update the constant height constraint of a UIView programmatically?

后端 未结 9 2320
执笔经年
执笔经年 2020-11-27 11:16

I have a UIView and I set the constraints using Xcode Interface Builder.

Now I need to update that UIView instance\'s height constant progra

9条回答
  •  悲&欢浪女
    2020-11-27 12:18

    Change HeightConstraint and WidthConstraint Without creating IBOutlet.

    Note: Assign height or width constraint in Storyboard or XIB file. after fetching this Constraint using this extension.

    You can use this extension to fetch a height and width Constraint:

    extension UIView {
    
    var heightConstraint: NSLayoutConstraint? {
        get {
            return constraints.first(where: {
                $0.firstAttribute == .height && $0.relation == .equal
            })
        }
        set { setNeedsLayout() }
    }
    
    var widthConstraint: NSLayoutConstraint? {
        get {
            return constraints.first(where: {
                $0.firstAttribute == .width && $0.relation == .equal
            })
        }
        set { setNeedsLayout() }
    }
    
    }
    

    You can use:

    yourView.heightConstraint?.constant = newValue 
    

提交回复
热议问题