【问题标题】:When added to a view, the constraint's items must be descendants of that view (or the view itself).添加到视图时,约束的项必须是该视图(或视图本身)的后代。
【发布时间】:2017-12-25 04:48:54
【问题描述】:

这是我的以下代码:

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = UIView()
    headerView.backgroundColor = UIColor.white
    let headerLabel = UILabel(frame: CGRect(x: 30, y: 0, width:
      tableView.bounds.size.width, height: tableView.bounds.size.height))
    headerLabel.font = UIFont().robotoMedium(withFontSize: 10)
    headerLabel.textColor = CustomColor.lightGrey.color
    headerLabel.text = "Travel Shops"
    headerLabel.sizeToFit()
    headerView.addSubview(headerLabel)
    headerLabel.translatesAutoresizingMaskIntoConstraints = false

    headerLabel.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .trailing, relatedBy: .equal, toItem: tableView, attribute: .trailing, multiplier: 1, constant: 0))
    headerLabel.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .leading, relatedBy: .equal, toItem: tableView, attribute: .leading, multiplier: 1, constant: 0))

    headerLabel.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .top, relatedBy: .equal, toItem: self.topLayoutGuide, attribute: .bottom, multiplier: 1, constant: 0))
    headerLabel.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 131))
    return headerView
  }

但应用程序崩溃并出现以下情况:

添加到视图时,约束项必须是该视图(或视图本身)的后代。如果在组装视图层次结构之前需要解决约束,这将崩溃。中断 -[UIView(UIConstraintBasedLayout) _viewHierarchyUnpreparedForConstraint:] 进行调试。

【问题讨论】:

    标签: ios xcode uitableview swift3


    【解决方案1】:

    headerLabelheaderView 的子视图,headerView 是层次结构中TableView 的子视图,这就是为什么显示此消息的原因,您需要将约束添加到headerView,即headerLabel.superView 而不是UITableView 是headerLabel.superView.superView

    更改您的代码

        headerLabel.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .trailing, relatedBy: .equal, toItem: headerView, attribute: .trailing, multiplier: 1, constant: 0))
        headerLabel.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .leading, relatedBy: .equal, toItem: headerView, attribute: .leading, multiplier: 1, constant: 0))
    
        headerLabel.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .top, relatedBy: .equal, toItem: self.topLayoutGuide, attribute: .bottom, multiplier: 1, constant: 0))
        headerLabel.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 131))
    

    这个人

        headerLabel.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .trailing, relatedBy: .equal, toItem: tableView, attribute: .trailing, multiplier: 1, constant: 0))
        headerLabel.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .leading, relatedBy: .equal, toItem: headerView, attribute: .leading, multiplier: 1, constant: 0))
    
        headerLabel.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .top, relatedBy: .equal, toItem: headerView, attribute: .bottom, multiplier: 1, constant: 0))
        headerLabel.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 131))
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-17
      • 1970-01-01
      • 2018-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-16
      相关资源
      最近更新 更多