![](/img/trans.png)
[英]Inserting collectionview inside cell of another collection view
[英]constraints of collection view cells inside another collectionview
我為UICollectionView創建了自定義類。 我想將另一個“集合”視圖放入第一類的單元格中。
import UIKit
class ModeCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
let modeCollection : UICollectionView = {
let layout = UICollectionViewFlowLayout()
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.backgroundColor = .cyan
collectionView.translatesAutoresizingMaskIntoConstraints = false
return collectionView
}()
func setupView(){
backgroundColor = .red
addSubview(modeCollection)
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[v0(180)]-20-|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": modeCollection]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-25-[v0(180)]", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": modeCollection]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-30-[v0(160)]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": modeCollection]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-95-[v0]", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": modeCollection]))
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
這段代碼可以正常工作,但是我想知道是否可以在其他項目中使用約束時使用約束:
模式收集。 .constraint(equalToConstant:49).isActive = true
問題是我的新自定義類中沒有“視圖”,並且我沒有什么要引用的。
而不是創建約束的view
,創建約束self
,這將參考UICollectionViewCell
本身( ModeCell
你的情況)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.