【问题标题】:How do I add constraints programmatically to UISegmentedControl如何以编程方式将约束添加到 UISegmentedControl
【发布时间】:2015-11-30 01:44:14
【问题描述】:

这是我的 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))

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

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

我做错了什么?

【问题讨论】:

    标签: ios swift uisegmentedcontrol


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2017-04-22
      • 2014-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-28
      • 1970-01-01
      相关资源
      最近更新 更多