我有UITableViewCell
具有UIlabel对齐的中心,我在UITableViewCell
的默认imageView属性中设置图像,但是由于文本居中对齐,因此文本和图像之间存在间隙。
[我想要图像然后留出很小的空间,然后将所有文本居中到UITableViewCell
我尝试了以下代码,
cell.imageView?.image = image
cell.imageView?.translatesAutoresizingMaskIntoConstraints = false
cell.imageView?.centerYAnchor.constraint(equalTo: label.centerYAnchor).isActive = true
let rect: CGRect = label.textRect(forBounds: label.bounds, limitedToNumberOfLines: 1)
cell.imageView?.leadingAnchor.constraint(equalTo: label.leadingAnchor, constant: rect.origin.x - padding).isActive = true
这对我有用,但是当我将设备从iPhone 11 Max Pro切换到iPhone 8时,图像会重叠文本,因为label.textRect
总是带来相同的文本,而与屏幕大小无关
我也尝试过使用第一个的范围,并且使用它的矩形,但是同样的问题是每个屏幕尺寸都不会更改。
是否可以在不将自定义UIImageView
放入UITableViewCell
的情况下实现?