2016-06-14 36 views
1

我想添加一些简单的约束,但是总是出现错误这样的:斯威夫特加约束,以上海华编程

Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to install constraint on view. Does the constraint reference something from outside the subtree of the view? That's illegal.

我的想法是增加了选择器视图成的tableview。当用户点击表格项时,这个选择器视图将显示。这里是我的代码:

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) 
    } 
} 

回答

1
self.view.addConstraint(topContrain) 
self.view.addConstraint(bottomContrain)