Swift - 布局 - 拉伸UILabel

时间:2017-08-17 07:26:24

标签: ios swift autolayout

我使用UILabelUIImageView

进行此布局

enter image description here

我希望UILabel具有最大可能长度,而UIIMageView最小。基本上,我希望“xxx”标签被拉伸并且具有与“text_1_____xxx”标签相同的宽度。怎么做到这一点?

元素名称为:

var textContentLabel: UILabel = {
    let lbl = UILabel()
    lbl.textAlignment = .right
    lbl.text = ""
    lbl.numberOfLines = 1
    lbl.adjustsFontSizeToFitWidth = true
    lbl.backgroundColor = UIColor.green
    lbl.translatesAutoresizingMaskIntoConstraints = false
    return lbl
}()

var textContentIcon: UIImageView = {
    let img = UIImageView()        
    img.image = nil
    img.backgroundColor = UIColor.orange
    img.contentMode = .scaleAspectFit
    img.translatesAutoresizingMaskIntoConstraints = false        
    return img
}()

我目前的主播是:

NSLayoutConstraint.activate([
            textContentLabel.topAnchor.constraint(equalTo: self.contentView.topAnchor),
            textContentLabel.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 0),
            textContentLabel.trailingAnchor.constraint(equalTo: textContentIcon.leadingAnchor, constant: 0),
            textContentLabel.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor),

            textContentIcon.topAnchor.constraint(equalTo: self.contentView.topAnchor),
            textContentIcon.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor, constant: 0),
            textContentIcon.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor)
        ])

修改

我正在寻找不基于标签“xxx”和“text_1 ______ xxx”之间设置约束的解决方案

3 个答案:

答案 0 :(得分:1)

我假设图像的大小可以根据内容而改变(否则你可以通过宽度/高度约束来设置它的尺寸。 这正是内容拥抱优先级的含义,它告诉autolayout引擎哪些元素最大限度地保持其大小并且想要扩展。要使用它,只需致电

setContentHuggingPriority(252, for: .horizontal)

在您的图片视图上(也可以通过故事板设置)。值252可以是您在标签上设置的值(默认值为250)。如果你有两个以上的元素,你可能需要稍微使用数字,如果你的布局更复杂,你可能还需要使用setContentCompressionResistancePriority

答案 1 :(得分:1)

请更新您的代码:

NSLayoutConstraint.activate([
            textContentLabel.topAnchor.constraint(equalTo: self.contentView.topAnchor),
            textContentLabel.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 0),
            textContentLabel.trailingAnchor.constraint(equalTo: textContentIcon.leadingAnchor, constant: 0),
            textContentLabel.heightAnchor.constraint(equalToConstant: 50),

            textContentIcon.topAnchor.constraint(equalTo: self.contentView.topAnchor),
            textContentIcon.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor, constant: 0),
            textContentIcon.heightAnchor.constraint(equalTo: 50)

            //Now your xxxLabel
            xxxLabel.topAnchor.constraint(equalTo: textContentLabel.bottomAnchor),
            xxxLabel.leadingAnchor.constraint(equalTo: textContentLabel.leadingAnchor, constant: 0),
            xxxLabel.trailingAnchor.constraint(equalTo: textContentLabel.trailingAnchor, constant: 0),
            xxxLabel.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor)


        ])

答案 2 :(得分:0)

如果你想让它们的宽度相同,那就有一个限制:

NSLayoutConstraint(item: label2, attribute: .width, relatedBy: .equal, toItem: label1, attribute: .width, multiplier: 1.0, constant: 0.0)

这使得label2的宽度始终等于label1的宽度。