UITableViewCell中的方形UIImageView和UITextField

时间:2019-03-03 13:26:42

标签: swift uitableview uiimageview uitextfield constraints

我正在尝试创建一个包含方形UIImageView和UITextField的单元格。如果文本适合屏幕的宽度,则一切正常。但是,一旦我将文本更改为更长的文本,演示文稿就会崩溃。控制台中没有关于约束的正确性的消息。

当UITextField中的文本适合屏幕显示或UITextField仅包含占位符时,一切正常。

screenshot when all works fine

但是,如果文本足够长,则演示文稿会崩溃。

enter image description here

用于创建视图的代码:

extension TextFieldWithImageTVCell {

    private func initView() {
        selectionStyle = .none
        initImgView()
        initTextField()
        contentView.addSubview(imgView)
        contentView.addSubview(textField)

        // debug code
        imgView.backgroundColor = UIColor.black
        textField.backgroundColor = UIColor.blue
    }

    private func initImgView() {
        imgView = UIImageView(frame: CGRect.zero)
        imgView.translatesAutoresizingMaskIntoConstraints = false
    }

    private func initTextField() {
        textField = UITextField(frame: CGRect.zero)
        textField.translatesAutoresizingMaskIntoConstraints = false
        textField.isUserInteractionEnabled = false
    }

}

我用来创建约束的代码:

extension TextFieldWithImageTVCell {

    private func initConstraints() {
        initConstraintsImgView()
        initConstraintsTextField()
    }

    private func initConstraintsImgView() {
        imgView.heightAnchor.constraint(equalTo: imgView.widthAnchor).isActive = true
        let margins = contentView.layoutMarginsGuide
        imgView.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
        imgView.topAnchor.constraint(equalTo: margins.topAnchor).isActive = true
        imgView.bottomAnchor.constraint(equalTo: margins.bottomAnchor).isActive = true
    }

    private func initConstraintsTextField() {
        textField.leadingAnchor.constraint(equalTo: imgView.trailingAnchor, constant: 8).isActive = true
        let margins = contentView.layoutMarginsGuide
        textField.topAnchor.constraint(equalTo: margins.topAnchor).isActive = true
        textField.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
        textField.bottomAnchor.constraint(equalTo: margins.bottomAnchor).isActive = true
    }

}

我用来配置单元格的代码部分:

cell.textContent = subject
cell.placeholder = "Предмет"
cell.isEditingEnabled = true

guard let textField = cell.textField else {
    assertionFailure("error in ItemSubject::configureCellForRow")
    return
}

textField.delegate = self
textField.clearButtonMode = .always
textField.adjustsFontSizeToFitWidth = true
textField.minimumFontSize = 10

单元格属性:

extension TextFieldWithImageTVCell {

    var placeholder: String? {
        set { textField.placeholder = newValue }
        get { return textField.placeholder }
    }

    var textContent: String? {
        set { textField.text = newValue }
        get { return textField.text }
    }

    var isEditingEnabled: Bool {
        set { textField.isUserInteractionEnabled = newValue }
        get { return textField.isUserInteractionEnabled }
    }

}

我需要添加哪些约束才能获得所需的结果? 为什么我的约束还不够?

0 个答案:

没有答案