【问题标题】:UIView subview autolayout issueUIView 子视图自动布局问题
【发布时间】:2016-04-28 13:48:38
【问题描述】:

我有 UITableView Cell,其中 4 个 UIView 像这样以垂直顺序排列,

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

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

我得到的是这样的,

处理约束的代码

    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];

【问题讨论】:

  • 您也想隐藏“收件人”标签吗?
  • @TejaNandamuri 是的
  • 当你改变一个约束时你需要调用 layoutIfNeeded 方法。
  • 不是为两个标签提供两个高度约束,而是将 uiview 高度约束设置为 0 并在需要时调用布局。
  • @TejaNandamuri 是的,我已将 IBOutlet 用于 UIView 的高度,并在没有可用数据时将其更改为 0。我也尝试过 layoutIfNeeded 但没有运气。我在 cellForRow 方法中这样做。

标签: ios objective-c uitableview uiview autolayout


【解决方案1】:

如果您的目标是 iOS 9+,我建议使用 UIStackView,因为只需将视图的 .hidden 属性设置为 true,它就可以满足您的需求。

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

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

【讨论】:

    【解决方案2】:

    以编程方式设置约束并根据需要编写逻辑是个好主意,不要忘记设置 layoutifnedded。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-17
      • 2016-04-04
      • 2014-02-05
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多