gpt4 book ai didi

ios - 当 UITextView 大小发生变化时,如何使 UITableViewCell 调整大小?

转载 作者:行者123 更新时间:2023-11-29 05:39:29 26 4
gpt4 key购买 nike

我遇到了 SnapKit 的自动布局问题。我将 UITextView 添加到 UITableViewCell,并且希望当用户添加内容时单元格随着 TextView 的扩展而扩展。然而,实际上,当 TextView 尝试扩展时,单元格约束就会被打破。这是我的代码:

TextFieldTableViewCell:

import UIKit

class TextFieldTableViewCell: FieldTableViewCell {

let textView = EditableTextView()

override func setUpViews() {
super.setUpViews()

setUpTextView()
}

private func setUpTextView() {
contentView.addSubview(textView)

textView.setContentHuggingPriority(UILayoutPriority(rawValue: 10000), for: .vertical)
textView.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 10000), for: .vertical)

textView.snp.makeConstraints { make in
make.top.equalTo(fieldLabel.snp.bottom).offset(margin)
make.leading.equalToSuperview().offset(margin)
make.trailing.equalToSuperview().offset(-margin)
make.bottom.equalToSuperview().offset(-margin)
}
}

}

可编辑 TextView :

import UIKit

class EditableTextView: UITextView {

override func didMoveToSuperview() {
super.didMoveToSuperview()

isEditable = true
isScrollEnabled = false
}

}

这是我的错误:

2019-06-20 18:23:10.276724-0500 Campaign Detective[1127:169808] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2019-06-20 18:23:10.278517-0500 Campaign Detective[1127:169808] [MC] Reading from public effective user settings.
2019-06-20 18:23:13.479469-0500 Campaign Detective[1127:169808] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSContentSizeLayoutConstraint:0x2831c90e0 Campaign_Detective.EditableTextView:0x140813200'Yeah I\U2019ll call them later...'.height == 86 Hug:10000 CompressionResistance:10000 (active)>",
"<SnapKit.LayoutConstraint:0x2831f82a0@FieldTableViewCell.swift#37 UILabel:0x13fb03540.top == UITableViewCellContentView:0x13fb03830.top + 15.0>",
"<SnapKit.LayoutConstraint:0x2831d1740@TextFieldTableViewCell.swift#20 Campaign_Detective.EditableTextView:0x140813200.top == UILabel:0x13fb03540.bottom + 15.0>",
"<SnapKit.LayoutConstraint:0x2831d1920@TextFieldTableViewCell.swift#23 Campaign_Detective.EditableTextView:0x140813200.bottom == UITableViewCellContentView:0x13fb03830.bottom - 15.0>",
"<NSLayoutConstraint:0x2836b5040 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x13fb03830.height == 123.333 (active)>"
)

Will attempt to recover by breaking constraint
<NSContentSizeLayoutConstraint:0x2831c90e0 Campaign_Detective.EditableTextView:0x140813200'Yeah I'll call them later...'.height == 86 Hug:10000 CompressionResistance:10000 (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.

单元格永远不会调整大小,因为 UITableViewCellContentView:0x13fb03830.height 约束不会更改。难道系统不应该将其更改为新计算的高度吗?当我为每个约束设置 .priority(.high) 时,它仍然不起作用。

最佳答案

下面一行告诉引擎,当空间不足时,您希望 textView 缩小。可能这不是你想要的

textView.setContentHuggingPriority(UILayoutPriority(rawValue: 10000), for: .vertical)

如果你设置了tableView.rowHeight = UITableView.automaticDimension,用户编辑完成后直接调用即可

tableView.beginUpdates()
tableView.endUpdates()

这应该重新计算单元格的高度并更新它

关于ios - 当 UITextView 大小发生变化时,如何使 UITableViewCell 调整大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56695063/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com