gpt4 book ai didi

ios - 如何使用自动布局以编程方式表示此 UITableCell 的内容?

转载 作者:行者123 更新时间:2023-11-28 22:26:25 24 4
gpt4 key购买 nike

我刚开始在我的最新项目中使用自动布局,我想知道布局以下表格单元格的最有效方式是什么:

UITableCell: the various necessary views are shown in black

View A 和 B 都是 UILabel。 C 是固定大小的图像,A 下的 View 是可能存在或不存在的图像。我可以轻松地布置 A、B 和 C。但是如果存在 A 下方的图像,则 A 的高度需要按比例缩小并且图像需要适合下方,以便两者在 contentView 中水平居中。

我正在尝试使用代码和视觉格式语言来布置整个单元格,到目前为止已经非常接近了。唯一的问题是 A 和它的伴随图像没有在容器中垂直居中。您可以在下图中看到我已经走了多远:

Layout using a combination of the Visual Format Language and some extra constraints

这是我在 updateConstraints 方法中使用的代码。请注意,使用此代码,我不会得到模棱两可的布局:

NSDictionary *views = NSDictionaryOfVariableBindings(viewA, viewB, viewC);
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[viewA]-[viewB]-(>=8)-[viewC]-|"
options:0
metrics:nil
views:views]];


NSDictionary *metrics = @{@"width":@(40.0f), @"height":@(40.0f), @"priority":@(UILayoutPriorityRequired)};

[viewC addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[viewC(==width@priority)]"
options:0
metrics:metrics
views:@{@"viewC": _merchantLogo}]];
[viewC addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[viewC(==height@priority)]"
options:0
metrics:metrics
views:views]];

[viewA addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[viewA(>=75@750)]"
options:0
metrics:nil
views:views]];

[viewB addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[viewB(>=115@500)]"
options:0
metrics:nil
views:views]];

[self.contentView addConstraints:@[[NSLayoutConstraint constraintWithItem:viewC
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.contentView
attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f],
[NSLayoutConstraint constraintWithItem:viewB
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.contentView
attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f]]];

if (!viewD) {
[self.contentView addConstraints:@[[NSLayoutConstraint constraintWithItem:viewA
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.contentView
attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f]]];
} else {
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[viewA][viewD]"
options:NSLayoutFormatAlignAllLeft
metrics:nil
views:NSDictionaryOfVariableBindings(viewA, viewD)]];
}

我的一个想法是将 A 及其下方的图像放在一个容器 View 中,然后将它们放置在该 View 中。但这似乎有点低效,我首先想确保在不使用容器 View 的情况下这是不可能的。

最佳答案

enter image description here

所以...

1.
Format
@"|-[_viewA(<=75)]-[viewB]-[viewC(==60)]-|"
Options
NSLayoutFormatAlignAllTop

2.
Format
@"V:|-[viewA]-[imageView(==10)]-|"
Options
NSLayoutFormatAlignCenterX | NSLayoutFormatAlignAllLeft

3.
Add individual constraints to constrain bottom of image view to bottom of viewB and viewC.
[NSLayotuConstraint constraintWithItem:viewB
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:imageView
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0];

and other one...

这应该给你你想要的。

关于ios - 如何使用自动布局以编程方式表示此 UITableCell 的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18824489/

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