【问题标题】:UITableViewCell contentView insets and Auto LayoutUITableViewCell contentView 插图和自动布局
【发布时间】:2018-12-17 13:46:38
【问题描述】:

我的问题是,我想为每个顶部、底部、左侧、右侧将单元格的 contentView 插入 10。我想将此空间用作分隔符并禁用默认的表格视图分隔符。它看起来像没有插图:

现在有两个问题:

  1. 问题:不明确的高度约束(在视图调试器中)
  2. 无分隔符

1.

  1. 我可以通过覆盖我的表格视图单元格的 layoutSubviews 来更改此设置:

    override func layoutSubviews() {
       super.layoutSubviews()
    
       let contentViewFrame = self.contentView.frame
       let insetContentViewFrame = contentViewFrame.inset(by: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))
       self.contentView.frame = insetContentViewFrame
       self.selectedBackgroundView?.frame = insetContentViewFrame
    }
    

但是这会导致第 3 个问题:

现在描述标签有问题:

这是我的表格视图单元格(它使用自动布局,因此标签定义了单元格高度):

class NewsTableViewCell : UITableViewCell {

public private(set) var titleLabel: UILabel!
public private(set) var descriptionLabel: UILabel!

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

    self.tintColor = UIColor.white

    self.contentView.backgroundColor = UIColor.barTintColor
    self.contentView.layer.masksToBounds = false
    self.contentView.layer.cornerRadius = 10.0

    self.backgroundColor = UIColor.clear

    let layoutGuide = self.contentView.layoutMarginsGuide

    self.titleLabel = UILabel(frame: .zero)
    self.titleLabel.numberOfLines = 0
    self.titleLabel.font = UIFont.preferredFont(forTextStyle: .headline)
    self.titleLabel.textColor = UIColor.white
    self.titleLabel.adjustsFontSizeToFitWidth = false
    self.titleLabel.translatesAutoresizingMaskIntoConstraints = false
    self.titleLabel.textAlignment = .left
    self.contentView.addSubview(self.titleLabel)

    self.titleLabel.setContentCompressionResistancePriority(.required, for: .vertical)

    self.titleLabel.leadingAnchor.constraint(equalTo: layoutGuide.leadingAnchor).isActive = true
    self.titleLabel.trailingAnchor.constraint(equalTo: layoutGuide.trailingAnchor).isActive = true
    self.titleLabel.topAnchor.constraint(equalTo: layoutGuide.topAnchor).isActive = true

    self.descriptionLabel = UILabel(frame: .zero)
    self.descriptionLabel.numberOfLines = 0
    self.descriptionLabel.font = UIFont.preferredFont(forTextStyle: .subheadline)
    self.descriptionLabel.textColor = UIColor.lightGray
    self.descriptionLabel.adjustsFontSizeToFitWidth = false
    self.descriptionLabel.translatesAutoresizingMaskIntoConstraints = false
    self.descriptionLabel.textAlignment = .left
    self.contentView.addSubview(self.descriptionLabel)

    self.descriptionLabel.setContentCompressionResistancePriority(.required, for: .vertical)

    self.descriptionLabel.leadingAnchor.constraint(equalTo: layoutGuide.leadingAnchor).isActive = true
    self.descriptionLabel.trailingAnchor.constraint(equalTo: layoutGuide.trailingAnchor).isActive = true
    self.descriptionLabel.topAnchor.constraint(equalTo: self.titleLabel.bottomAnchor, constant: 5.0).isActive = true
    self.descriptionLabel.bottomAnchor.constraint(equalTo: layoutGuide.bottomAnchor).isActive = true

    let selectedView: UIView = UIView(frame: .zero)
    selectedView.backgroundColor = UIColor.gray
    selectedView.layer.cornerRadius = 10.0
    selectedView.layer.masksToBounds = false
    self.selectedBackgroundView = selectedView
}

这是我的表格视图控制器代码:

self.tableView.rowHeight = UITableView.automaticDimension
self.tableView.estimatedRowHeight = 200.0
self.tableView.register(NewsTableViewCell.self, forCellReuseIdentifier: "NewsCell")
self.tableView.separatorStyle = .none

有什么方法可以同时插入我的 contentView 和动态调整大小的标签(定义单元格高度)? 谢谢!

【问题讨论】:

    标签: ios swift uitableview


    【解决方案1】:

    1- 添加常量参数

     self.titleLabel.leadingAnchor.constraint(equalTo: layoutGuide.leadingAnchor,constant:10).isActive = true
    

    用于前导、尾随、顶部和底部

    2-

    descriptionLabel底部约束的优先级降低到999

    【讨论】:

    • 1.这会更改标签而不是内容视图!内容视图具有与单元格不同的背景颜色,我想要间距 2. 工作的标签不再剪辑,但单元格也不会随它调整大小
    • 谢谢,这成功了!我将 titleLabel 和 descriptionLabel 的 contentCompressionResistancePriority 设置为必需,如果我的 layoutSubviews 底部插入为 10,我的 descriptionLabel 底部约束的常量为 -20
    【解决方案2】:

    你的问题

    1 ) 您希望两个标签动态增长。
    2)您希望您的单元格根据您的内容调整大小

    为此,您需要在所有侧面(左侧、前导、尾迹、底部)进行约束,否则自动尺寸将不起作用

    但自动布局引擎不允许这样做。因为它需要一些特定的想法来确定哪个标签会增长,因此它可以在内部计算。和 。您在更改内容拥抱优先级时遇到错误

    解决方法也很简单

    假设从底部开始你需要 8px 空间然后应用大于等于( >=) 与 8 的关系

    并将您的内容拥抱优先级重置为他们现在不需要的默认值:)

    【讨论】:

      猜你喜欢
      • 2018-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-29
      • 2020-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多