2017-08-17 39 views
2

我有这样的布局UILabelUIImageView斯威夫特 - 布局 - 伸展的UILabel

enter image description here

我想UILabel有最大可能的长度,而UIIMageView最小。基本上,我想要“xxx”标签被拉伸并且具有与“text_1_____xxx”标签相同的宽度。如何实现这一目标?

元素被inited为:

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”

+0

编辑问题 –

+0

由于text_1______xxx的宽度被限制计算,我不认为我们可以给宽度XXX标签的精确值,所以我想你会如果您希望它们具有相同的宽度,则需要此约束 – 3stud1ant3

回答

1

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

setContentHuggingPriority(252, for: .horizontal) 

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

0

如果你想让它们的宽度相同,那么就有一个约束为此:

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

这使得它的标签2的宽度始终等于label1的宽度。

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) 


     ])