【问题标题】:What is the best approach to autolayout dynamic size uitableview cells自动布局动态大小 uitableview 单元格的最佳方法是什么
【发布时间】:2015-02-20 09:56:47
【问题描述】:

我目前被困在尝试创建一个带有自动布局的 uitableview 单元格,该单元格可以根据

中的“动态大小标签”动态调整大小

我的方法是子类化 uitableview 单元格,并在“updateConstraints”方法中我覆盖:

CGFloat width = [[UIScreen mainScreen] bounds].size.width;
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView
                                                                 attribute:NSLayoutAttributeWidth
                                                                 relatedBy:NSLayoutRelationEqual
                                                                    toItem:nil
                                                                 attribute:NSLayoutAttributeNotAnAttribute
                                                                multiplier:1
                                                                constant:width]];
CGFloat height = self.frame.size.height;
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView
                                                                 attribute:NSLayoutAttributeHeight
                                                                 relatedBy:NSLayoutRelationEqual
                                                                    toItem:nil
                                                                 attribute:NSLayoutAttributeNotAnAttribute
                                                                multiplier:1
                                                                  constant:height]];;

[super updateConstraints];

同时,我在 nib 文件中进行了所有布局,其中子视图的大小已正确调整,但单元格高度未调整大小。

我在这里犯的新手错误是什么?

【问题讨论】:

  • 您的目标是哪个 iOS 版本?另外,编辑问题,以便我们可以更轻松地阅读您发布的代码。
  • 你读过this tutorial吗?这是一个适合新手的好网站。
  • 我的目标是 iOS 7

标签: ios objective-c iphone uitableview autolayout


【解决方案1】:

显然我在睡觉或发生了什么变化

[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView
                                                             attribute:NSLayoutAttributeHeight
                                                             relatedBy:NSLayoutRelationGreaterThanOrEqual
                                                                toItem:nil
                                                             attribute:NSLayoutAttributeNotAnAttribute
                                                            multiplier:1
                                                              constant:height]];;

【讨论】:

    猜你喜欢
    • 2019-05-09
    • 1970-01-01
    • 1970-01-01
    • 2014-04-06
    • 2019-07-22
    • 1970-01-01
    • 2015-05-25
    • 2014-09-02
    • 1970-01-01
    相关资源
    最近更新 更多