uitableviewcell 以编程方式使用自动布局更新约束

tableviewcell using AutoLayout update constraint programmatically

我有一个使用 Autolayout 的自定义 tableviewcell,在 xib 文件中设计。

设计:

CustomTableViewCell.h: @property (weak, nonatomic) IBOutlet UIView *customView; @property (weak, nonatomic) IBOutlet NSLayoutConstraint *constraintRatio;

它在表格视图中工作正常。

我想做的是在 tableviewcell 显示之前以编程方式更新 CustomView 的比率。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ CustomTableViewCell *customCell = [self.mainTableView dequeueReusableCellWithIdentifier:@"customCell"]; customCell.constraintRatio.constant = 2; [customCell setNeedsUpdateConstraints]; [customCell setNeedsLayout]; [customCell layoutIfNeeded]; return customCell; }

但此后单元格的比率保持为 1。 我做错了什么或错过了什么?感谢您的任何建议。

问题已解决。

我误用了 customCell.constraintRatio.constant。这应该是乘数。

multiplier 是只读的。所以完成我的工作的唯一方法是删除约束并添加新约束

这样做之后,一切正常。

参考: