【问题标题】:Custom UITableViewCells in iOS 8 and layoutMarginiOS 8 中的自定义 UITableViewCells 和 layoutMargin
【发布时间】:2014-09-23 04:18:48
【问题描述】:

我的应用中有几个自定义的 UITableViewCell,主要由 nib 定义。移至 iOS 8 和 Xcode 6 时,左右边距不正确。这些单元格通常散布在带有默认单元格的表格视图中。

我做了一个示例项目,这是我正在谈论的保证金问题:

我能找到的唯一与此相关的是新属性layoutMargins。对于 UITableViewCells,它的值似乎会根据应用运行的设备而变化:

iPhone 6 and below - layoutMargin: {8, 16, 8, 16}
iPhone 6 Plus - layoutMargin: {8, 20, 8, 20}

这似乎与我在标准单元格上看到的边距一致。但是,我的自定义单元格的内容在单元格contentView 内,它具有{8, 8, 8, 8} 的标准 UIView layoutMargin。这意味着绑定到容器边距的任何自动布局约束都添加了不正确的间距。

我发现解决此问题的唯一方法是在 cellForRowAtIndexPath: 中添加以下内容

cell.contentView.layoutMargins = cell.layoutMargins;

这似乎不是一个很好的解决方案(特别是因为我需要将它包装在 iOS8 的检查中以保持兼容性)。

有人有什么想法吗?我觉得我一定是错过了什么。

【问题讨论】:

    标签: objective-c uitableview ios8


    【解决方案1】:

    您可能需要查看 preservesSuperviewLayoutMargins 属性。这听起来像您正在寻找的东西。

    将以下内容添加到任何需要与标准单元格保持一致间距的单元格中:

    - (void)layoutSubviews {
        [super layoutSubviews];
    
        if ([self.contentView respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
            self.contentView.preservesSuperviewLayoutMargins = YES;
        }
    }
    

    【讨论】:

    • 谢谢!这就是我一直在寻找的。​​span>
    【解决方案2】:

    在您的 UITableViewCell 子类中,覆盖 layoutMarginsDidChange 并设置 contentView 的 layoutMargins 以匹配单元格的 layoutMargins:

    - (void)layoutMarginsDidChange {
        contentView.layoutMargins = layoutMargins
    }
    

    我发现这比将preservesSuperviewLayoutMargins 设置为YES 更可靠。

    【讨论】:

      【解决方案3】:

      preservesSuperviewLayoutMargins 为 iOS8 解决了这个问题。下面的代码包括它,以及为 ios7.1 解决它的附加代码。

      class CustomTableViewCell: UITableViewCell {
      
          @IBOutlet weak var mainLabel: UILabel!
      
          override func layoutSubviews() {
              super.layoutSubviews()
      
              if contentView.respondsToSelector(Selector("preservesSuperviewLayoutMargins")) {
                  contentView.preservesSuperviewLayoutMargins = true
              } else {
                  if mainLabel != nil {
                      let leftConstraint = NSLayoutConstraint(item: mainLabel,
                          attribute: .Leading,
                          relatedBy: .Equal,
                          toItem: contentView,
                          attribute: .Leading,
                          multiplier: 1.0,
                          constant: 16.0);
                      addConstraint(leftConstraint);
                  }
              }
          }
      
      }
      

      【讨论】:

        【解决方案4】:

        要在 iOS 8+ 中删除那个时髦的左边距,在你的表格视图中:

        table.separatorInset = UIEdgeInsets.zero     // or UIEdgeInsetsZero
        

        您可能还需要:

        table.layoutMargins = UIEdgeInsets.zero
        

        归功于https://stackoverflow.com/a/30640595/385273

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-01-29
          • 2014-10-01
          • 2020-08-04
          • 1970-01-01
          • 1970-01-01
          • 2014-08-11
          • 1970-01-01
          相关资源
          最近更新 更多