为什么单元格中的自定义分隔符视图会改变高度?

Why the custom separator view in the cell changes height?

我有自定义的 tableViewCell。在它的单元格中,我有自定义分隔符。工作逻辑应该是这样的:

  1. if select 单元格中的文本字段 - 分隔符更改颜色和 高度(从 1 到 2)
  2. 如果键入文本 - 分隔符颜色和高度不是 变化

现在它的工作方式如下:

  1. 如果未在 .xib 中为分隔符添加高度约束 - 在创建单元格时添加了高度约束,但在屏幕中它等于 0
override func awakeFromNib() {
    customSeparator.backgroundColor = .lightGray
    customSeparator.frame.size.height = 1.0
}
  1. 如果在 .xib 中添加了对分隔符高度的约束 - 当 select 分隔符的单元格高度发生变化时(如预期)。但是当键入文本高度更改为 .xib
  2. 中指定的值时
func textFieldDidBeginEditing(_ textField: UITextField) {
    customSeparator.backgroundColor = .black
    customSeparator.frame.size.height = 2.0       
}

所以,为什么会这样,告诉我,plz

您需要创建一个高度 IBOutlet 约束并更改如下:

@IBOutlet weak var customSeparatorHeight: NSLayoutConstraint!

override func awakeFromNib() {
    customSeparator.backgroundColor = .lightGray
    customSeparatorHeight.constant = 1
    self.contentView.layoutIfNeeded()
}