方向更改时自定义UITableViewCell自动布局问题

问题描述 投票:0回答:1

我有一个像这样的自定义UITableViewCell类:

class CustomCell: UITableViewCell {
  var mainText:UILabel = UILabel()
  var detailText:UILabel = UILabel()

  override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    self.contentView.addSubview(mainText)
    self.contentView.addSubview(detailText)

    self.updateCellConstraints()
  }

  fileprivate func updateCellConstraints() {
    self.mainText.translatesAutoresizingMaskIntoConstraints = false
    self.detailText.translatesAutoresizingMaskIntoConstraints = false
    self.detailText.setContentCompressionResistancePriority(.required, for: .horizontal)

    let margins = self.contentView.layoutMarginsGuide

    self.mainText.leftAnchor.constraint(equalTo: margins.leftAnchor).isActive = true
    self.mainText.centerYAnchor.constraint(equalTo: margins.centerYAnchor).isActive = true

    self.detailText.leftAnchor.constraint(equalTo: self.mainText.rightAnchor, constant: 10).isActive = true
    self.detailText.widthAnchor.constraint(greaterThanOrEqualToConstant: 20).isActive = true
    self.detailText.rightAnchor.constraint(equalTo: margins.rightAnchor, constant: -10).isActive = true
    self.detailText.centerYAnchor.constraint(equalTo: margins.centerYAnchor).isActive = true
  }
}

cellForRowAtIndexPath我将它出列并在mainTextdetailText上设置了一些自定义属性,最后设置了一个accessoryType,这是一个披露指标。

现在问题。一切都在纵向模式下运行良好但是当我在iPad上切换到横向时,我的单元格居中(即左侧和右侧区域留空,单元格居中,maintextdisclosureIndicator仍然分开但UI看起来不太好)。我怎么摆脱这个?

我希望我的手机能够像在纵向模式下一样完美地工作。难道我做错了什么?

ios swift uitableview autolayout
1个回答
2
投票

这是因为Apple在iOS9中添加了可读的内容指南。

做就是了:

tableView.cellLayoutMarginsFollowReadableWidth = false

或者如果你部署到iOS9之前:

if #available(iOS 9.0, *) {
    tableView.cellLayoutMarginsFollowReadableWidth = false
}

有关此主题的更多信息Readable Width of Table View Cells

Both modes side by side

© www.soinside.com 2019 - 2024. All rights reserved.