iOS - 使用居中对齐的 UILabel 文本约束 UIImage
iOS - Constraint to UIImage with centrally aligned UILabel's text
我 UITableViewCell
有一个 UIlabel 居中对齐 我在 UITableViewCell
的默认 imageView 属性 中设置图像,但由于文本居中对齐,文本之间有间隙和形象。
我想要图片然后是小 space 然后文本全部居中到 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
是否可以实现?
您可以使用在单元格内居中的 stackView,并将 imageView 和标签添加为已排列的子视图。请注意,您需要创建自定义单元格。
创建您的堆栈视图:
let stackView: UIStackView = {
let stackView = UIStackView()
stackView.axis = .horizontal
stackView.alignment = .center
stackView.distribution = .fill
stackView.spacing = 10 // You can set the spacing accordingly
return stackView
}()
布局如下:
contentView.addSubview(stackView)
// Swap these two lines if instead you want label then image
stackView.addArrangedSubview(image)
stackView.addArrangedSubview(label)
// StackView
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.centerYAnchor.constraint(equalTo: self.contentView.centerYAnchor).isActive = true
stackView.centerXAnchor.constraint(equalTo: self.contentView.centerXAnchor).isActive = true
我 UITableViewCell
有一个 UIlabel 居中对齐 我在 UITableViewCell
的默认 imageView 属性 中设置图像,但由于文本居中对齐,文本之间有间隙和形象。
我想要图片然后是小 space 然后文本全部居中到 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
是否可以实现?
您可以使用在单元格内居中的 stackView,并将 imageView 和标签添加为已排列的子视图。请注意,您需要创建自定义单元格。
创建您的堆栈视图:
let stackView: UIStackView = { let stackView = UIStackView() stackView.axis = .horizontal stackView.alignment = .center stackView.distribution = .fill stackView.spacing = 10 // You can set the spacing accordingly return stackView }()
布局如下:
contentView.addSubview(stackView) // Swap these two lines if instead you want label then image stackView.addArrangedSubview(image) stackView.addArrangedSubview(label) // StackView stackView.translatesAutoresizingMaskIntoConstraints = false stackView.centerYAnchor.constraint(equalTo: self.contentView.centerYAnchor).isActive = true stackView.centerXAnchor.constraint(equalTo: self.contentView.centerXAnchor).isActive = true