gpt4 book ai didi

ios - 无法约束将 UIView 固定到单元格 contentView 的底部

转载 作者:行者123 更新时间:2023-11-29 00:37:33 24 4
gpt4 key购买 nike

在我的应用程序的 UITableViewController 的 cellForRowAtIndexPath: 方法中,我需要将一个编程 UIView 固定到每个单元格各自的 cell.contentView 的底部。这是我的代码:

separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320,15)];
separatorLineView.tag = 17;
separatorLineView.backgroundColor = [UIColor colorWithHexString:@"F0F5F7"];
[cell.contentView addSubview:separatorLineView];
[self.separatorLineView setTranslatesAutoresizingMaskIntoConstraints:NO];

NSDictionary* views = NSDictionaryOfVariableBindings(separatorLineView);
NSString *format = @"V:[separatorLineView]-|";


positionYConstraint = [NSLayoutConstraint constraintsWithVisualFormat:format
options:0
metrics:nil
views:views];

heightConstraint = [NSLayoutConstraint constraintWithItem:postSeparatorLineView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:cell.contentView
attribute:NSLayoutAttributeHeight
multiplier:0.01
constant:15];


[cell.contentView addConstraints:positionYConstraint];
[cell.contentView addConstraint:heightConstraint];
[cell.contentView layoutSubviews];

当使用 setTranslatesAuto...:NO 行运行时,我看不到 separatorLineView,甚至在 View 调试器中也看不到。当我注释掉该行时,separatorLineView 被设置到 cell.contentView 的顶部。

基本上我需要代码做的就是将 separatorLineView 固定到每个单元格的 contentView 的底部 - 请记住我有动态单元格高度。

最佳答案

你需要水平方向的约束,否则它不会知道它的宽度和x位置。检查这个:

UIView * separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320,15)];
separatorLineView.tag = 17;
separatorLineView.backgroundColor = [UIColor colorWithHexString:@"F0F5F7"]; [cell.contentView addSubview:separatorLineView];
[separatorLineView setTranslatesAutoresizingMaskIntoConstraints:NO];

NSDictionary* views = NSDictionaryOfVariableBindings(separatorLineView);
NSString *formatV = @"V:[separatorLineView(==15)]|"; // changed this line to set 15 as height and to really pin your view (note I remove the "-" because it adds a 8 margin)


NSArray *positionYConstraint = [NSLayoutConstraint constraintsWithVisualFormat:formatV
options:0
metrics:nil
views:views];

NSString *formatH = @"H:|[separatorLineView]|"; //these are the missing constraints
NSArray *positionXConstraint = [NSLayoutConstraint constraintsWithVisualFormat:formatH
options:0
metrics:nil
views:views];


[cell.contentView addConstraints:positionYConstraint];
[cell.contentView addConstraints:positionXConstraint];

关于ios - 无法约束将 UIView 固定到单元格 contentView 的底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40274538/

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