我正在水平堆栈视图左侧的图像视图和一个行数设置为0的标签。问题是即使我已经设置了堆栈视图分布,imageview和label也填充相等的空间财产填补。我不希望我的图片视图扩展。改变内容拥抱并没有帮助。
答案 0 :(得分:2)
答案 1 :(得分:0)
另一种方法是在图像+标签堆栈的右侧添加一个不可见的间隔物对象。
private func spacer() -> UIView {
let stretchingView = UIView()
stretchingView.setContentHuggingPriority(.defaultHigh, for: .horizontal)
stretchingView.backgroundColor = UIColor.clear
stretchingView.translatesAutoresizingMaskIntoConstraints = false
return stretchingView
}
private func labelWithImage() -> UIView {
let image = UIImage(systemName: "map")
let imageView = UIImageView(image: image)
let label = UILabel()
label.text = "3 Miles Away"
label.font = UIFont.systemFont(ofSize: 17, weight: .regular)
label.textColor = UIColor.gray
let stackView = UIStackView(arrangedSubviews: [imageView, label, spacer()])
stackView.alignment = .leading
stackView.axis = .horizontal
return stackView
}