ios - UIView subview 自动布局问题

标签 ios objective-c uitableview uiview autolayout

我有一个 UITableView Cell,其中 4 个 UIView 按这样的垂直顺序排列,

enter image description here

在每个 UIView 中,我有两个像这样的标签,

enter image description here

我已经为所有四个 UIView 的高度约束采用了 IBOutlet,并且当该 View 中的数据不可用时,我想使 View 的高度为 0。问题是由于该 View 内 UILabel 的 10 像素底部约束, View 未获得 0 高度。

我得到的是这样的,

enter image description here

处理约束的代码

    NSString *toName = [self jointNameFromArray:evidence.arrTo];
    if ([Utility isEmptyString:toName]) {
        cell.toViewHeight.constant = 0;
    }
    else {
        cell.lblToName.text = toName;
    }

    NSString *fromName = [self jointNameFromArray:evidence.arrFrom];
    if ([Utility isEmptyString:fromName]) {
        cell.fromViewHeight.constant = 0;
    }
    else {
        cell.lblFromName.text = fromName;
    }

    NSString *ccName = [self jointNameFromArray:evidence.arrCc];
    if ([Utility isEmptyString:ccName]) {
        cell.ccViewHeight.constant = 0;
    }
    else {
        cell.lblCCName.text = ccName;
    }

    NSString *custName = [self jointNameFromArray:evidence.arrCustodian];
    if ([Utility isEmptyString:custName]) {
        cell.custViewHeight.constant = 0;
    }
    else {
        cell.lblCustName.text = custName;
    }    
     [cell layoutIfNeeded];

最佳答案

如果您的目标是 iOS 9+,我建议使用 UIStackView,因为它只需设置 View 的 .hidden 属性即可完全满足您的需求为true

https://www.raizlabs.com/dev/2016/04/uistackview/

如果您无法使用UIStackView,则需要将垂直填充约束之一设置为低于高度约束的优先级。您还需要将 .clipsToBounds 设置为 true

关于ios - UIView subview 自动布局问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36916678/

相关文章:

ios - Swift - 通过网络调用和 Storyboard 在 View Controller 中进行依赖注入(inject)?

ios - 如何动画和旋转按钮

ios - 如何使用指向MTLBuffer Swift的指针进行reinterpret_cast

iphone - Xcode 构建脚本来分离图像 channel ?

ios - UIStackView 没有将自身对齐到中心?对象C

iphone - 如何以编程方式显示 UITableView?

java - 与 Java 相比,Objective C 中的术语和概念有哪些不同的名称?

objective-c - 在线 xcode 项目(一起工作)

ios - 为什么两个 TableView 单元格中的两个 Collection View 在 Swift 4 中不起作用?

ios - 如何在用户看到 ios View 之前加载数据?