在UITableviewCell中使用自动布局

4

我正在尝试使用SnapKit在Swift中自动调整UITableView单元格大小!UITableView上设置的相关标志如下:

self.rowHeight = UITableViewAutomaticDimension
self.estimatedRowHeight = 70.0

我的自定义UITableviewCell类中有一个UITextField,如下所示:

var uidTextField: UITextField = UITextField()

而我自定义的UITableViewCell中文本字段的初始设置如下:

self.contentView.addSubview(uidTextField)
uidTextField.attributedPlaceholder = NSAttributedString(string: "Woo Hoo", attributes: [NSForegroundColorAttributeName:UIColor.lightGrayColor()])
uidTextField.textAlignment = NSTextAlignment.Left
uidTextField.font = UIFont.systemFontOfSize(19)
uidTextField.returnKeyType = UIReturnKeyType.Done
uidTextField.autocorrectionType = UITextAutocorrectionType.No
uidTextField.delegate = self
uidTextField.addTarget(self, action: "uidFieldChanged", forControlEvents: UIControlEvents.EditingChanged)
uidTextField.snp_makeConstraints { make in 
    make.left.equalTo(self.contentView).offset(10)
    make.right.equalTo(self.contentView)
    make.top.equalTo(self.contentView).offset(10)
    make.bottom.equalTo(self.contentView).offset(10)
}

当我执行代码时,它显示被截断并在控制台中给出以下错误:
警告:检测到一种情况,其中约束模糊地建议表视图单元格内容视图的高度为零。我们考虑折叠是无意的,并使用标准高度代替。
我的autoLayout约束有问题还是这是一个与UIControls和UITableView单元格自动调整大小有关的问题?
1个回答

2
在SnapKit(和Masonry)中,您必须使用负值向视图的右侧或底部添加填充。您正在使用offset(10)在您的bottom约束上,这会导致您的文本字段底部的10pt被剪切掉。
要解决此问题,您必须给底部约束一个负偏移量:
uidTextField.snp_makeConstraints { make in 
    make.left.equalTo(self.contentView).offset(10)
    make.right.equalTo(self.contentView)
    make.top.equalTo(self.contentView).offset(10)
    make.bottom.equalTo(self.contentView).offset(-10)
}

或者您可以通过以下方式获得相同的约束条件:

uidTextField.snp_makeConstraints { make in 
    make.edges.equalTo(contentView).inset(UIEdgeInsetsMake(10, 10, 10, 0))
}

当您使用inset()方法时,必须对右侧和底部插图使用正值。
我不理解为什么SnapKit在底部和右侧使用负值。我认为这是反直觉的,有点令人困惑。 编辑:这是一个小例子,可以正常工作(我硬编码了一个带有3个自定义单元格的tableView,其中包括一个UITextField): ViewController:
import UIKit
import SnapKit

class ViewController: UIViewController {
    let tableView = UITableView()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(tableView)
        tableView.dataSource = self
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 70
        tableView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier: "CustomCell")

        tableView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(view)
        }
    }
}

extension ViewController: UITableViewDataSource {
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath)
        return cell
    }
}

自定义表格单元格:

import UIKit

class CustomTableViewCell: UITableViewCell {
    let textField = UITextField()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        textField.attributedPlaceholder = NSAttributedString(string: "Woo Hoo", attributes: [NSForegroundColorAttributeName:UIColor.lightGrayColor()])
        textField.textAlignment = NSTextAlignment.Left
        textField.font = UIFont.systemFontOfSize(19)
        textField.returnKeyType = UIReturnKeyType.Done
        textField.autocorrectionType = UITextAutocorrectionType.No
        contentView.addSubview(textField)

        textField.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).inset(UIEdgeInsetsMake(10, 10, 10, 0))
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

你好,很抱歉今天没有及时回复你。然而,当使用插入而不是偏移来调整大小时,单元格仍然无法正确地调整大小。出现了相同的错误: 检测到约束模糊地建议表视图单元格内容视图的高度为零。我们认为这是意外的折叠,并使用标准高度代替。 虽然我确实认为你在误用偏移方面是正确的。我仍在研究这个问题。 - CWineland
您的 UITextField 高度是否固定? - joern
它没有固有的或基于文本高度的高度,我原以为它会有。我将尝试给它一个高度。 - CWineland
尝试通过为contentView设置高度约束来覆盖系统的高度约束:make.height.equalTo(uidTextField) - joern
我尝试重现您遇到的错误,但一切都按预期工作,没有任何错误。请查看我编辑后的答案,其中包含我使用的代码。也许您正在做一些不同的事情? - joern
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接