如何为IBDesignable自定义UI添加约束到故事板

时间:2018-01-28 07:28:05

标签: ios uibutton custom-controls ibdesignable

我创建了IBDesignable自定义UIButton,这是我的按钮:

enter image description here

然后我添加约束:

self.translatesAutoresizingMaskIntoConstraints =  false
let leftConstrain = NSLayoutConstraint.init(item: self, attribute: NSLayoutAttribute.leading, relatedBy: NSLayoutRelation.equal, toItem: superview, attribute: NSLayoutAttribute.leadingMargin, multiplier: 1.0, constant: 8)
let rightConstrain = NSLayoutConstraint.init(item: self, attribute: NSLayoutAttribute.trailing, relatedBy: NSLayoutRelation.equal, toItem: superview, attribute: NSLayoutAttribute.trailingMargin, multiplier: 1.0, constant: -8)
let bottomConstrain = NSLayoutConstraint.init(item: self, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: superview, attribute: NSLayoutAttribute.bottomMargin, multiplier: 1.0, constant: -8)
let height = NSLayoutConstraint.init(item: self, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1.0, constant: 60)

NSLayoutConstraint.activate([leftConstrain, rightConstrain, bottomConstrain, height])

在我添加约束后,它会是这样的:

enter image description here

问题是按钮的框架,视图高度和位置已更改,但框架没有变化且没有约束。

如何强制框架与我添加的约束兼容?

0 个答案:

没有答案