问题描述
我正在使用新的UICollectionViewListCell
和UIContentConfiguration
,它们很棒,但是我不知道如何更改UIKit
的默认单元格高度44自动为这些单元格设置。这是一个示例单元格:
class TextFieldCell: UICollectionViewListCell {
struct ContentConfiguration: UIContentConfiguration,Hashable {
struct Handlers: ContentConfigurationHandlers {
var onTextChanged: ((_ newText: String) -> Void)?
}
var handlers: Handlers?
var text: String?
var placeholder: String?
var keyboardType: UIKeyboardType?
func makeContentView() -> UIView & UIContentView {
ContentView(configuration: self)
}
func updated(for state: UIConfigurationState) -> TextFieldCell.ContentConfiguration {
guard let state = state as? UICellConfigurationState else { return self }
var updatedConfiguration = self
return updatedConfiguration
}
}
class ContentView: UIView,UIContentView,UITextFieldDelegate {
private var appliedConfiguration: TextFieldCell.ContentConfiguration!
var configuration: UIContentConfiguration {
get {
appliedConfiguration
}
set {
guard let newConfiguration = newValue as? TextFieldCell.ContentConfiguration else {
return
}
apply(configuration: newConfiguration)
}
}
let nameTextField: UITextField = UITextField()
init(configuration: TextFieldCell.ContentConfiguration) {
super.init(frame: .zero)
setupInternalViews()
apply(configuration: configuration)
}
required init?(coder: NSCoder) {
fatalError()
}
private func setupInternalViews() {
addSubview(nameTextField)
nameTextField.delegate = self
nameTextField.clearButtonMode = .always
nameTextField.autocapitalizationType = .sentences
nameTextField.addAction(UIAction(handler: { [unowned self] action in
textChanged()
}),for: .editingChanged)
NSLayoutConstraint.activate([
nameTextField.leadingAnchor.constraint(equalTo: leadingAnchor),nameTextField.topAnchor.constraint(equalTo: topAnchor),nameTextField.trailingAnchor.constraint(equalTo: trailingAnchor),nameTextField.bottomAnchor.constraint(equalTo: bottomAnchor),nameTextField.heightAnchor.constraint(equalToConstant: 200) // Example height
])
}
/// Apply a new configuration.
/// - Parameter configuration: The new configuration
private func apply(configuration: TextFieldCell.ContentConfiguration) {
guard appliedConfiguration != configuration else { return }
appliedConfiguration = configuration
nameTextField.text = appliedConfiguration.text
nameTextField.placeholder = appliedConfiguration.placeholder
nameTextField.keyboardType = appliedConfiguration.keyboardType ?? .default
nameTextField.autocapitalizationType = .sentences
}
private func textChanged() {
appliedConfiguration.handlers?.onTextChanged?(nameTextField.text ?? "")
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}
}
重要的部分是设置文本字段的布局约束:
NSLayoutConstraint.activate([
nameTextField.leadingAnchor.constraint(equalTo: leadingAnchor),nameTextField.heightAnchor.constraint(equalToConstant: 200) // Example height
])
我将高度设置为200(例如)。当我运行它时,我得到了:
2020-11-08 11:12:01.805610+0100 Sophia[11924:720407] [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.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand,refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0x600001111f40 h=--& v=--& _TtCC6Sophia13TextFieldCell11ContentView:0x7fc0ed27a1c0.height == 44 (active)>","<NSLayoutConstraint:0x6000011198b0 UITextField:0x7fc0ed27a370.height == 200 (active)>","<NSLayoutConstraint:0x600001119810 V:|-(0)-[UITextField:0x7fc0ed27a370] (active,names: '|':_TtCC6Sophia13TextFieldCell11ContentView:0x7fc0ed27a1c0 )>","<NSLayoutConstraint:0x600001119b80 UITextField:0x7fc0ed27a370.bottom == _TtCC6Sophia13TextFieldCell11ContentView:0x7fc0ed27a1c0.bottom (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x6000011198b0 UITextField:0x7fc0ed27a370.height == 200 (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.
如您所见,有一个自动生成的高度约束将高度设置为44
,这会产生冲突。为什么在那里?我该如何更改?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)