以编程方式设置collectionview单元格中标签约束的高度

时间:2021-05-31 22:43:30

I am trying to setup height constraint for a label in collectionview cell

我正在尝试为collectionview单元格中的标签设置高度约束

   class CollectionViewCell: UICollectionViewCell {

   @IBOutlet weak var label: UILabel!
   override func layoutSubviews() {
   super.layoutSubviews()
      constraint()
    }
   func constraint() {

        label.addConstraint(NSLayoutConstraint(item:label, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 25))
    }
}

I did the above and it is not working. Does the layoutSubviews declaration work here.

我做了上面的事情并没有奏效。 layoutSubviews声明是否适用于此处。

2 个解决方案

#1


2  

There is a convenience way to use constraint and change it in code.

有一种方便的方法来使用约束并在代码中更改它。

First, declare a constraint property:

首先,声明一个约束属性:

@IBOutlet weak var labelHeight: NSLayoutConstraint!

Second, bind it in XIB or Stroyboard: 以编程方式设置collectionview单元格中标签约束的高度

其次,将它绑定在XIB或Stroyboard中:

Finally, you are able to change it in programming way:

最后,您可以通过编程方式更改它:

self.labelHeight.constant = 130

#2


0  

NSLayoutConstraint(item: label, attribute: .Height, relatedBy: .Equal, toItem: label, attribute:.Height, multiplier: 1.0, constant:25.0)

or

要么

NSLayoutConstraint.constraintsWithVisualFormat(@"V:[label(==24)]", options: nil , metrics: nil, views: NSDictionaryOfVariableBindings(label))

https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/ProgrammaticallyCreatingConstraints.html

https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/ProgrammaticallyCreatingConstraints.html

(...)you must specify a value for each parameter, even if it doesn’t affect the layout. The end result is a considerable amount of boilerplate code, which is usually harder to read. (...)

(...)您必须为每个参数指定一个值,即使它不影响布局。最终结果是大量的样板代码,通常难以阅读。 (......)

#1


2  

There is a convenience way to use constraint and change it in code.

有一种方便的方法来使用约束并在代码中更改它。

First, declare a constraint property:

首先,声明一个约束属性:

@IBOutlet weak var labelHeight: NSLayoutConstraint!

Second, bind it in XIB or Stroyboard: 以编程方式设置collectionview单元格中标签约束的高度

其次,将它绑定在XIB或Stroyboard中:

Finally, you are able to change it in programming way:

最后,您可以通过编程方式更改它:

self.labelHeight.constant = 130

#2


0  

NSLayoutConstraint(item: label, attribute: .Height, relatedBy: .Equal, toItem: label, attribute:.Height, multiplier: 1.0, constant:25.0)

or

要么

NSLayoutConstraint.constraintsWithVisualFormat(@"V:[label(==24)]", options: nil , metrics: nil, views: NSDictionaryOfVariableBindings(label))

https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/ProgrammaticallyCreatingConstraints.html

https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/ProgrammaticallyCreatingConstraints.html

(...)you must specify a value for each parameter, even if it doesn’t affect the layout. The end result is a considerable amount of boilerplate code, which is usually harder to read. (...)

(...)您必须为每个参数指定一个值,即使它不影响布局。最终结果是大量的样板代码,通常难以阅读。 (......)