Swift,添加带约束的按钮以编程方式查看

时间:2016-03-21 11:43:39

标签: ios swift iphone uitableview

我在tableviewCell中有一个视图,我想添加按钮并为视图添加此按钮的约束
喜欢facebook的问题

1 个答案:

答案 0 :(得分:1)

因为你的问题不是很明确,我只能放弃这个:

YOURVIEW.addSubview(YOURBUTTON)

iOS 9 +

YOURVIEW.addSubview(YOURBUTTON)

YOURBUTTON.translatesAutoresizingMaskIntoConstraints = false
YOURBUTTON.topAnchor.constraintEqualToAnchor(YOURVIEW.topAnchor).active = true
YOURBUTTON.bottomAnchor.constraintEqualToAnchor(YOURVIEW.bottomAnchor).active = true
YOURBUTTON.leadingAnchor.constraintEqualToAnchor(YOURVIEW.leadingAnchor).active = true       
YOURBUTTON.trailingAnchor.constraintEqualToAnchor(YOURVIEW.trailingAnchor).active = true

iOS 6 +

let view = ["yourButotn": YOURBUTTON]
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[yourButotn]-|", options: [], metrics: nil, views: views))
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[yourButotn]-|", options: [], metrics: nil, views: views))

这将添加约束