我正在尝试以编程方式创建一个按钮并使用 SnapKit 应用约束。一切正常,但是当我应用约束时make.center.equalTo(self.view)
它崩溃给我这个消息“由于未捕获的异常而终止应用程序无法安装约束,原因:视图之间没有共同的超级视图”。这是我创建按钮的代码:
func createButton() {
let button = UIButton(type: UIButtonType.System) as UIButton
button.backgroundColor = UIColor.greenColor()
button.setTitle("Test Button", forState: UIControlState.Normal)
button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
button.snp_makeConstraints { (make) -> Void in
make.width.equalTo(200)
make.height.equalTo(70)
make.center.equalTo(self.view)
}
self.view.addSubview(button)
}