自定义UITableViewCell的自动布局在Swift 5中发现冲突的约束

问题描述 投票:0回答:1

我遇到以下问题。我当时正在设计一个具有Autolayout的自定义UITableViewCell设计,并且一切正常。 Xcode没有显示任何错误。

但是当我确实在模拟器或实际的iPhone上运行该应用程序时,我在控制台中收到以下错误代码:

2020-04-16 17:14:36.928201+0200 Promille[11911:1025126] [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. 
(
    "<NSLayoutConstraint:0x600000ddc460 UIStackView:0x7f8bba72bb20.height == 75   (active)>",
    "<NSLayoutConstraint:0x600000ddc7d0 V:[UIStackView:0x7f8bba72bb20]-(10)-|   (active, names: '|':UITableViewCellContentView:0x7f8bba712390 )>",
    "<NSLayoutConstraint:0x600000ddc870 V:|-(10)-[UIStackView:0x7f8bba72bb20]   (active, names: '|':UITableViewCellContentView:0x7f8bba712390 )>",
    "<NSLayoutConstraint:0x600000dde0d0 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7f8bba712390.height == 95.5   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x600000ddc460 UIStackView:0x7f8bba72bb20.height == 75   (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.

错误与高度有关,因为一旦删除高度约束,一切都会正常运行。但是,在这种情况下,两个图像均为全尺寸,并且TableViewCell的高度大于1000px。

这里也是.xib文件的图像:enter image description here

真正奇怪的是,我在运行时没有更改任何约束。仅文本标签被更改。那么如何自动布局会发现错误,而在设计布局时却找不到自动布局呢?


您知道错误消息中的最后一个约束是什么,因为我没有设置内容视图的高度。

<NSLayoutConstraint:0x600000dde0d0 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7f8bba712390.height == 95.5   (active)>"

希望你们能想到出现此问题的原因/解决方案。

ios swift uitableview autolayout nslayoutconstraint
1个回答
0
投票

您可以以此image shows约束故事板中的所有单元格。

然后您可以在此方法中设置tableviewcell的高度

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 75
}

然后,您无需在情节提要中设置单元格的高度。例如,您还可以在此处使用动态高度,使其达到整个屏幕尺寸的一半。

要添加此方法,您需要遵守

extension ViewController: UITableViewDelegate, UITableViewDataSource 

tableviewdelegate不需要此功能,但可以将其添加到其他tableview方法中。不要忘记设置委托]

tableview.delegate = self
© www.soinside.com 2019 - 2024. All rights reserved.