什么都没发生,但为什么???
@IBOutlet weak var button10: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
button10.center = CGPoint(x: self.view.frame.x/2, y: self.view.frame.y/2)
}
答案 0 :(得分:1)
为视图中心添加约束
var constX = NSLayoutConstraint(item: button10, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
view.addConstraint(constX)
var constY = NSLayoutConstraint(item: button10, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)
view.addConstraint(constY)
答案 1 :(得分:0)
正如Ashhish所说,在父视图和按钮之间添加两个约束。但这也可以使用令人敬畏的Github项目Cartography来完成:
layout(view, button) {
parent, button in
button.centerX == parent.centerX
button.centerY == parent.centerY
}
非常整洁!
您可以使用Cocoapods包含制图。