我想添加一些简单的约束,但总是会出现这样的错误:
由于未捕获的异常“NSGenericException”而终止应用程序,原因:“无法在 View 上安装约束。约束是否引用了 View 子树之外的内容?这是非法的。
我的想法是将选择器 View 添加到 TableView 中。当用户单击表项时,将显示此选择器 View 。这是我的代码:
class InscriptionTableViewController: UITableViewController, UIPickerViewDelegate, UIPickerViewDataSource {
var pickView:UIPickerView!
override func viewDidLoad() {
pickView = UIPickerView()
pickView.delegate = self
pickView.dataSource = self
pickView.hidden = true
pickView.backgroundColor = UIColor.whiteColor()
self.view.addSubview(pickView)
let topContrain = NSLayoutConstraint(
item: pickView,
attribute: NSLayoutAttribute.Top,
relatedBy: NSLayoutRelation.Equal,
toItem: self.view,
attribute: NSLayoutAttribute.Top,
multiplier: 1,
constant: 0)
let bottomContrain = NSLayoutConstraint(
item: pickView,
attribute: NSLayoutAttribute.Bottom,
relatedBy: NSLayoutRelation.Equal,
toItem: self.view,
attribute: NSLayoutAttribute.Bottom,
multiplier: 1,
constant: 0)
pickView.addConstraint(topContrain)
pickView.addConstraint(bottomContrain)
}
}
最佳答案
self.view.addConstraint(topContrain)
self.view.addConstraint(bottomContrain)
关于ios - Swift 以编程方式向 super View 添加约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37823762/