gpt4 book ai didi

ios - 使用 NSLayoutConstraint constraintWithVisualFormat 在基本 UITableVewCell 中设置 ImageView

转载 作者:行者123 更新时间:2023-11-29 12:35:19 26 4
gpt4 key购买 nike

我必须在不使用任何 xib 或 Storyboard的情况下完成此 UIview 子类,它需要 1 个文件,因此我需要在代码中设置此 tableview 并避免子类化 UITableViewCell。

我正在尝试将 ImageView 设置为V:|-5-[ ImageView ]-5-|和 H:[iamgeview(width = height)]-5-|在单元格的 contentView 中。

我得到的只是我添加的所有约束都被破坏了,单元格不在我想要的位置。

base cell是基本样式的cell。我想要做的就是在 View 的末尾添加一个 ImageView ,这正在变成一场噩梦。

我在代码中按如下方式设置 ImageView 。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

if (!cell)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
POI *poi = [self.results objectAtIndex:indexPath.row];
NSString *text = poi.name;

cell.textLabel.text = text;

UIImageView *image = [[UIImageView alloc] init];
image.image = [UIImage imageWithColor:[UIColor blueColor]];
[cell.contentView addSubview:image];
NSDictionary* viewDic = @{@"image":image};
cell.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
[cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[image]"
options:0
metrics:nil
views:viewDic]];

[cell.contentView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:[image(w)]-5-|"
options:0
metrics:@{@"w":cell.contentView.frame.size.heigh}
views:viewDic]];

return cell;

当单元格加载时我得到这个错误:

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7fb7b636aaa0 H:[img(44)] (Names: img:0x7fb7b6368fc0 )>

<NSLayoutConstraint:0x7fb7b6369380 V:[img(44)] (Names: img:0x7fb7b6368fc0 )>

<NSLayoutConstraint:0x7fb7b636aaf0 H:[img]-(5)-| (Names: Contentview:0x7fb7b6372a30, img:0x7fb7b6368fc0, '|':Contentview:0x7fb7b6372a30 )>

<NSLayoutConstraint:0x7fb7b63697e0 V:|-(5)-[img] (Names: img:0x7fb7b6368fc0, Contentview:0x7fb7b6372a30, '|':Contentview:0x7fb7b6372a30 )>

最佳答案

必须为任何使用自动布局约束的 View 设置translatesAutoresizingMaskIntoConstraints=NO。如果您不这样做,与 autoResizingMask 匹配的自动布局约束将自动添加到 View 中,除了您手动添加的任何约束。这几乎总是会导致过度约束 View ,其中必须打破一个或多个约束才能布局 View 。

关于ios - 使用 NSLayoutConstraint constraintWithVisualFormat 在基本 UITableVewCell 中设置 ImageView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26529920/

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