How to add constraints programmatically using Swift

前端 未结 17 1161
逝去的感伤
逝去的感伤 2020-11-21 23:07

I\'m trying to figure this out since last week without going any step further. Ok, so I need to apply some constraints programmatically

17条回答
  •  伪装坚强ぢ
    2020-11-21 23:20

        var xCenterConstraint : NSLayoutConstraint!
        var yCenterConstraint: NSLayoutConstraint!
    
     xCenterConstraint = NSLayoutConstraint(item: self.view, attribute: .CenterX, relatedBy: .Equal, toItem: (Your view NAme), attribute: .CenterX, multiplier: 1, constant: 0)
                self.view.addConstraint(xCenterConstraint)
    
     yCenterConstraint = NSLayoutConstraint(item: self.view, attribute: .CenterY, relatedBy: .Equal, toItem: (Your view Name), attribute: .CenterY, multiplier: 1, constant: 0)
                self.view.addConstraint(yCenterConstraint)
    

提交回复
热议问题