简体   繁体   English

如何以编程方式将约束从标签添加到 UICollectionViewCell?

[英]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.

相关问题 如何在UICollectionViewCell内部的标签上添加约束? - How to add constraints to a label inside of a UICollectionViewCell? 在UICollectionViewCell中以编程方式设置布局约束 - Programmatically setting layout constraints in a UICollectionViewCell 如何以编程方式将宽高比约束应用于自定义UICollectionViewCell? - How do I apply aspect ratio constraints to a custom UICollectionViewCell programmatically? 如何以编程方式将UILabel添加到我的UICollectionViewCell - How to programmatically add an UILabel to my UICollectionViewCell 如何以编程方式在Swift 3中将UILabel添加到UICollectionViewCell - How to programmatically add a UILabel to UICollectionViewCell in Swift 3 如何在UICollectionViewCell上设置约束? - How to set constraints on a UICollectionViewCell? 如何以编程方式为Rangeslider添加约束? - how to add constraints programmatically for rangeslider? 如何在ios中以编程方式添加约束 - How to add constraints programmatically in ios 如何以编程方式将约束添加到以编程方式创建的UIView? - How to add constraints programmatically to a programmatically created UIView? 如何以编程方式在 TableCell 中添加图像和添加约束? - how to add Images and add constraints in TableCell programmatically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM