Width and Height Equal to its superView using autolayout programmatically?

前端 未结 11 1457
后悔当初
后悔当初 2020-12-07 09:09

I\'ve been looking for a lot of snippets in the net and I still can\'t find the answer to my problem. My question is I have a scrollView(SV) and I want to add a button insid

11条回答
  •  北海茫月
    2020-12-07 09:43

    Swift 4 using NSLayoutConstraint:

    footerBoardImageView.translatesAutoresizingMaskIntoConstraints = false
    let widthConstraint  = NSLayoutConstraint(item: yourview, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: superview, attribute: NSLayoutAttribute.width, multiplier: 1, constant: 0)
    let heightConstraint = NSLayoutConstraint(item: yourview, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: superview, attribute: NSLayoutAttribute.height, multiplier: 1, constant: 0)
    superview.addConstraints([widthConstraint, heightConstraint])
    

提交回复
热议问题