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

标签 ios objective-c cocoa autolayout nslayoutconstraint

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

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/

相关文章:

ios - Xamarin 低功耗蓝牙

cocoa - 如何从 cocoa 发送HTML电子邮件?

cocoa - 如何让我的(非基于文档的)应用程序响应打开文件 :withApplication:?

ios - 我可以模态呈现作为导航 Controller 一部分的 View Controller 吗?

ios - 当我以编程方式添加一个没有约束的 subview 时会发生什么?

ios - iPad 上 Springboard 中应用程序标题的字体大小

objective-c - 如何在 objective-c 中将字节值转换为 int

ios - 如果json数据为零,如何在tableview中隐藏单元格

objective-c - 如何在 UINavigationController 标题 View 中自定义后退按钮的文本颜色?

objective-c - HTML 到 NSString