[英]How to programmatically add constraints from label to UICollectionViewCell?
I have a question regarding constraints to a UICollectionViewCell.我有一个关于 UICollectionViewCell 约束的问题。
An image is included to help visualize what I mean.包含一个图像以帮助形象化我的意思。
I have a UICollectionViewController.我有一个 UICollectionViewController。 Inside this controller I have added a UICollectionViewCell, which only takes up a part of the screen.
在这个控制器中,我添加了一个 UICollectionViewCell,它只占屏幕的一部分。
My question is: How do I add constraints from a label (see image) to that UICollectionViewCell, such that I know that the label is always the same distance from the UICollectionViewCell.我的问题是:如何从标签(见图片)向该 UICollectionViewCell 添加约束,以便我知道标签与 UICollectionViewCell 的距离始终相同。 In the image the distance is shown as an arrow (this is the constraint, that I want to add).
在图像中,距离显示为一个箭头(这是我想要添加的约束)。
Hope someone can help me out.希望有人可以帮助我。
Thanks in advance.提前致谢。
Assuming the label is a subview from the CollectionViewController
, it can be done like:假设标签是来自
CollectionViewController
的子视图,可以这样做:
label.topAnchor.constraint(equalTo: cell.bottomAnchor, constant: someConstant).isActive = true
The above code works for me at least with an UITableViewController
and its cells, so it should also work with a UICollectionView
.上面的代码至少适用于
UITableViewController
及其单元格,因此它也应该适用于UICollectionView
。
First at all you should know what constraints you have to set.首先,您应该知道必须设置哪些约束。 In this case I guess it is top, trailing and leading if you want an infinite label.
在这种情况下,如果您想要无限标签,我想它是顶部、尾随和前导。 Else you have to set height constraint too.
否则你也必须设置高度约束。
Example of code for adding constraints for label in collectionViewCell:在 collectionViewCell 中为标签添加约束的代码示例:
label = UILabel()
self.addSubview(label)
label.translatesAutoresizingMaskIntoConstraints = false
// creating a top constraint related to cell content view top with constant 16
let topConstraint = NSLayoutConstraint(item: label, attribute: .top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: .top, multiplier: 1, constant: 16)
let leadingConstraint = NSLayoutConstraint(item: label, attribute: .leading, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: .leading, multiplier: 1, constant: 16)
let trailingConstraint = NSLayoutConstraint(item: label, attribute: .trailing, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: .trailing, multiplier: 1, constant: 16)
// optional constraint
//let heightConstaint = NSLayoutConstraint(item: label, attribute: .height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 44)
self.addConstraints([topConstraint, leadingConstraint, trailingConstraint])
If I understood your question right, you want to have similar relations between your collectionView and label.如果我理解你的问题,你希望在你的 collectionView 和标签之间有类似的关系。
So you should write the same code as I wrote above but relate your label constraints to collectionView.因此,您应该编写与我上面写的相同的代码,但将您的标签约束与 collectionView 相关联。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.