Ios UICollectionViewListCell始终将高度设置为44,该高度与我在AutoLayout中设置的任何其他高度冲突

Ios UICollectionViewListCell始终将高度设置为44,该高度与我在AutoLayout中设置的任何其他高度冲突,ios,swift,uicollectionview,autolayout,uicollectionviewcell,Ios,Swift,Uicollectionview,Autolayout,Uicollectionviewcell,我正在玩新的UICollectionViewListCell和UIContentConfigurations,它们非常棒,但我不知道如何更改UIKit为这些单元格自动设置的默认单元格高度44。下面是一个示例单元格: 类TextFieldCell:UICollectionViewListCell{ 结构ContentConfiguration:UIContentConfiguration,可哈希{ 结构处理程序:ContentConfigurationHandlers{ var onTextCh

我正在玩新的
UICollectionViewListCell
UIContentConfiguration
s,它们非常棒,但我不知道如何更改
UIKit
为这些单元格自动设置的默认单元格高度44。下面是一个示例单元格:


类TextFieldCell:UICollectionViewListCell{
结构ContentConfiguration:UIContentConfiguration,可哈希{
结构处理程序:ContentConfigurationHandlers{
var onTextChanged:((unewtext:String)->Void)?
}
变量处理程序:处理程序?
变量文本:字符串?
变量占位符:字符串?
var键盘类型:UIKeyboardType?
func makeContentView()->UIView和UIContentView{
ContentView(配置:self)
}
func更新(对于状态:UIConfigurationState)->TextFieldCell.ContentConfiguration{
guard let state=状态为?UICellConfigurationState else{return self}
var updatedConfiguration=self
返回更新的配置
}
}
类ContentView:UIView、UIContentView、UIExtFieldDelegate{
私有变量appliedConfiguration:TextFieldCell.ContentConfiguration!
变量配置:UIContentConfiguration{
得到{
应用配置
}
设置{
guard let newConfiguration=newValue as?TextFieldCell.ContentConfiguration else{
返回
}
应用(配置:新配置)
}
}
让nameTextField:UITextField=UITextField()
init(配置:TextFieldCell.ContentConfiguration){
super.init(帧:.0)
setupInternalViews()
应用(配置:配置)
}
必需初始化?(编码器:NSCoder){
法塔莱罗()
}
私有函数设置内部视图(){
addSubview(nameTextField)
nameTextField.delegate=self
nameTextField.clearButtonMode=.always
nameTextField.autocapitalizationType=
nameTextField.addAction(UIAction)(处理程序:{[unowned self]中的操作
textChanged()
}),用于:。编辑已更改)
NSLayoutConstraint.activate([
nameTextField.leadingAnchor.constraint(等于:leadingAnchor),
nameTextField.topAnchor.constraint(等于:topAnchor),
nameTextField.trailingAnchor.constraint(等式:trailingAnchor),
nameTextField.bottomAnchor.constraint(等号:bottomAnchor),
nameTextField.heightAnchor.constraint(equalToConstant:200)//高度示例
])
}
///应用新配置。
///-参数配置:新配置
私有函数应用(配置:TextFieldCell.ContentConfiguration){
guard appliedConfiguration!=配置else{return}
appliedConfiguration=配置
nameTextField.text=appliedConfiguration.text
nameTextField.placeholder=appliedConfiguration.placeholder
nameTextField.keyboardType=appliedConfiguration.keyboardType???。默认值
nameTextField.autocapitalizationType=
}
私有函数textChanged(){
appliedConfiguration.handlers?.ContextChanged?(nameTextField.text??)
}
func textField应该返回(textField:UITextField)->Bool{
textField.resignFirstResponder()辞职
返回真值
}
}
}
重要的部分是设置文本字段的布局约束:

NSLayoutConstraint.activate([
nameTextField.leadingAnchor.constraint(等于:leadingAnchor),
nameTextField.topAnchor.constraint(等于:topAnchor),
nameTextField.trailingAnchor.constraint(等式:trailingAnchor),
nameTextField.bottomAnchor.constraint(等号:bottomAnchor),
nameTextField.heightAnchor.constraint(equalToConstant:200)//高度示例
])
我将高度设置为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.
2020-11-08 11:12:01.805610+0100 Sophia[11924:720407][LayoutConstraints]无法同时满足约束。
可能下面列表中至少有一个约束是您不想要的。
试试这个:
(1) 看看每一个约束,试着找出你不期望的;
(2) 找到添加了不需要的约束的代码,然后修复它。
(注意:如果您看到不理解的NSAutoresizingMaskLayoutConstraints,请参阅UIView属性TranslatesAutoResizingMaskToConstraints的文档)
(
"",
"",
"",
""
)
将尝试通过打破约束进行恢复
在UIViewAlertForUnsatifiableConstraints处创建一个符号断点,以便在调试器中捕获该断点。
中列出的UIView上UIConstraintBasedLayoutDebugging类别中的方法也可能会有所帮助。

如您所见,有一个自动生成的高度约束将高度设置为
44
,这会产生冲突。为什么会这样?我怎样才能改变它呢?

我遇到了同样的问题。我现在能够为自己描述这一点的最好方式是,涉及
UIContentView
的约束需要有“多个可满足的解决方案”,其中您希望它拥有的解决方案只是所有可能的解决方案中的“最佳”方案

因此,在与您的情况基本相同的情况下,我通过将底部约束的优先级降低到
.defaultHigh
来解决这个问题。起初我认为将高度的优先级设置为一个较低的值就足够了,但后来它使用了该视图的
intrinsicContentSize
(在我的例子中是