如何以编程方式向UISegmentedControl添加约束

Reh*_*ani 2 uisegmentedcontrol ios swift

这是我的UISegmentedControl的代码:

   employeeSegmentedControl = UISegmentedControl(items: items)
    employeeSegmentedControl.selectedSegmentIndex = 0
    employeeSegmentedControl.backgroundColor = UIColor(red: 41/255, green: 128/255, blue: 185/255, alpha: 1.0)
    employeeSegmentedControl.tintColor = UIColor.whiteColor()
    employeeSegmentedControl.addTarget(self, action: Selector("indexChanged:"), forControlEvents: .ValueChanged)
    self.view.addSubview(employeeSegmentedControl)

    employeeSegmentedControl.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.view.addConstraint(NSLayoutConstraint(item: employeeSegmentedControl, attribute: .Width, relatedBy: .Equal, toItem: self.view, attribute: .Width, multiplier: 1.0, constant: 0.0))
    self.view.addConstraint(NSLayoutConstraint(item: employeeSegmentedControl, attribute: .Height, relatedBy: .Equal, toItem: self.searchController.searchBar, attribute: .Height, multiplier: 1.0, constant: 0.0))
    self.view.addConstraint(NSLayoutConstraint(item: employeeSegmentedControl, attribute: .CenterX, relatedBy: .Equal, toItem: self.view, attribute: .CenterX, multiplier: 1.0, constant: 0.0))
    self.view.addConstraint(NSLayoutConstraint(item: employeeSegmentedControl, attribute: .CenterY, relatedBy: .Equal, toItem: self.searchController.searchBar, attribute: .CenterY, multiplier: 1.2, constant: 0.0))
Run Code Online (Sandbox Code Playgroud)

如您所见,我正在向UISegmentedControl添加约束,但是我的应用程序崩溃了。这是我的错误信息:

2015-09-03 18:50:52.241 Employee Keeper [14424:11105086] *由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'* + [NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:] :0或零个第二项的乘数以及第一个属性的位置会创建位置等于常数的非法约束。必须在成对的***第一个throw调用堆栈中指定位置属性:

我究竟做错了什么?

ZYi*_*iOS 5

我认为这是因为self.searchController.searchBar设置约束时您为零