版权声明:大熊猫猪·侯佩原创或翻译作品.谢绝转载! hopy https://blog.csdn.net/mydo/article/details/84670867
由于UITableView中某一行文字过多,需要扩展Cell的高度,该Cell的样式是.subtitle:
注意其中的detailTextLabel并没有垂直居中!!!
咋一看解决应该很简单,做一个约束不就行了:
$0.cell.detailTextLabel?.snp.updateConstraints {make in
make.centerY.equalToSuperview()
make.right.equalToSuperview().offset(-16)
}
遗憾的是,这样做一旦用户按下该Cell,detailTextLabel的位置又会回到上部!
如上,当首次显示Cell时,其中的detailTextLabel可以在约束下正确定位,但在点击Cell后,detailTextLabel位置又上移回去了。
运行后在Xcode中动态检查detailTextLabel的约束设置,都是正确的。
我猜测是detailTextLabel的父视图contentView对其做了特殊控制,老是想将它限定在指定的位置上。
我的解决方法是取消contentView的自动约束:
$0.cell.contentView.translatesAutoresizingMaskIntoConstraints = false
然后重新对detailTextLabel参考其他元素重新做限制:
扫描二维码关注公众号,回复:
4353837 查看本文章
$0.cell.detailTextLabel?.snp.updateConstraints {make in
make.centerY.equalTo(cell.textLabel!)
make.right.equalTo(cell.snp.right).offset(-16)
}
这回detailTextLabel位置不再变动,实现了我们的要求!