【问题标题】:How to properly write the constraint code如何正确编写约束代码
【发布时间】:2018-03-02 06:37:26
【问题描述】:

我想在UITableViewCell 中设置一个 UITextView。 在 TableView 函数的某些方法中,我编写了以下代码, 但是它会导致以下错误:

由于未捕获的异常“NSGenericException”而终止应用程序, 原因:'无法使用锚激活约束 和 因为他们 没有共同的祖先。约束或其锚点是否引用 不同视图层次结构中的项目?这是非法的。'

这是我在 tableView 中的代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = newTableView.dequeueReusableCell(withIdentifier: "NewCell", for: indexPath) as! NewCellTableViewCell
    let tableTextView = UITextView()
    tableTextView.translatesAutoresizingMaskIntoConstraints = false
    tableTextView.leadingAnchor.constraint(equalTo: cell.leadingAnchor).isActive = true
    tableTextView.topAnchor.constraint(equalTo: cell.topAnchor).isActive = true
    tableTextView.widthAnchor.constraint(equalTo: cell.widthAnchor).isActive = true
    tableTextView.heightAnchor.constraint(equalTo: cell.heightAnchor).isActive = true
    cell.addSubview(tableTextView)
    return cell
}

【问题讨论】:

    标签: swift xcode swift4 xcode9


    【解决方案1】:

    首先你应该调用:

    cell.addSubview(tableTextView)
    

    然后附加约束,此外:

    所有约束必须只涉及范围内的视图 接收视图。具体来说,所涉及的任何观点都必须是 接收视图本身,或接收视图的子视图。

    所以你的代码可能是:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = newTableView.dequeueReusableCell(withIdentifier: "NewCell", for: indexPath) as! NewCellTableViewCell
        let tableTextView = UITextView()
        cell.addSubView(tableTextView)
        tableTextView.translatesAutoresizingMaskIntoConstraints = false
    
        tableTextView.leadingAnchor.constraint(equalTo: cell.leadingAnchor).isActive = true
        tableTextView.topAnchor.constraint(equalTo: cell.topAnchor).isActive = true
        tableTextView.widthAnchor.constraint(equalTo: cell.widthAnchor).isActive = true
        tableTextView.heightAnchor.constraint(equalTo: cell.heightAnchor).isActive = true
        return cell
    }
    

    【讨论】:

    • 感谢您的评论。这条评论解决了我的问题:)
    • 这个好像很不对,应该是cell.addSubview(tableTextView),最好是cell.contentView。您不想将单元格添加到文本视图中。
    • @Sulthan 这显然是 OP 的拼写错误,我解决了这个问题
    【解决方案2】:

    必须在应用约束之前将子视图添加到父视图:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = newTableView.dequeueReusableCell(withIdentifier: "NewCell", for: indexPath) as! NewCellTableViewCell
        let tableTextView = UITextView()
        tableTextView.translatesAutoresizingMaskIntoConstraints = false
        cell.addSubView(tableTextView)
        tableTextView.leadingAnchor.constraint(equalTo: cell.leadingAnchor).isActive = true
        tableTextView.topAnchor.constraint(equalTo: cell.topAnchor).isActive = true
        tableTextView.widthAnchor.constraint(equalTo: cell.widthAnchor).isActive = true
        tableTextView.heightAnchor.constraint(equalTo: cell.heightAnchor).isActive = true
    
        return cell
    }
    

    【讨论】:

    • 感谢您的评论。这条评论解决了我的问题:)
    • 实际上UITextView 似乎被添加为cell 的子视图。不是相反。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-22
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    相关资源
    最近更新 更多