另一个集合视图中集合视图单元格的约束
constraints of collection view cells inside another collectionview
我已经为 UICollectionView 创建了自定义 class。我想将另一个集合视图放入第一个 class 的单元格中。
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")
}
}
这段代码工作正常,但我想知道是否可以使用约束,因为我在其他项目中使用它们:
modeCollection.centerXAnchor.constraint(等于: .centerXAnchor).isActive = true
modeCollection.bottomAnchor.constraint(等于: view.bottomAnchor).isActive = true
modeCollection.widthAnchor.constraint(等于: view.widthAnchor).isActive = true
modeCollection.heightAnchor.constraint(equalToConstant: 49).isActive = true
问题是我的新习惯里没有"view" class 而且我也没有什么可以参考的...
您不是为 view
创建约束,而是为 self
创建约束,它将引用 UICollectionViewCell
本身(在您的情况下为 ModeCell
)。
我已经为 UICollectionView 创建了自定义 class。我想将另一个集合视图放入第一个 class 的单元格中。
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")
}
}
这段代码工作正常,但我想知道是否可以使用约束,因为我在其他项目中使用它们:
modeCollection.centerXAnchor.constraint(等于: .centerXAnchor).isActive = true modeCollection.bottomAnchor.constraint(等于: view.bottomAnchor).isActive = true modeCollection.widthAnchor.constraint(等于: view.widthAnchor).isActive = true modeCollection.heightAnchor.constraint(equalToConstant: 49).isActive = true
问题是我的新习惯里没有"view" class 而且我也没有什么可以参考的...
您不是为 view
创建约束,而是为 self
创建约束,它将引用 UICollectionViewCell
本身(在您的情况下为 ModeCell
)。