我想用Code中的自定义标头创建UICollectionView。因此,我创建了UICollectionViewCell的子类来描述我的自定义标头。我想在标题的水平线上显示五个标签。因此,我创建了五个标签并将其添加到stackView中。stackview显示我的标签被均等填充。到目前为止,一切都很完美。
标签1标签2标签3标签4标签5
//Label 1
let votesLabel: UILabel = {
let label = UILabel()
let attributedText = NSMutableAttributedString(string: "Votes", attributes: [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 12) ])
attributedText.append(NSAttributedString(string: "\n 0", attributes: [NSForegroundColorAttributeName: UIColor.lightGray, NSFontAttributeName: UIFont.systemFont(ofSize: 14)]))
label.attributedText = attributedText
// label.text = "11\nvotes"
label.textAlignment = .center
label.numberOfLines = 0
return label
}()
// label 2 ... 5
// creating the subview and add the labels to my subview
addSubview(topDividerView)
let stackView = UIStackView(arrangedSubviews: [ votesLabel, and the other 4 labels])
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis …Run Code Online (Sandbox Code Playgroud) 我以编程方式构建了一个 UICollectionViewController,它返回 4 个单元格。HomeCell、TrendingCell、SubscriptionCell 和 AccountCell。所有 4 个单元格应该不同,您可以水平滚动它们。<--->。
class HomeController: UICollectionViewController, UICollectionViewDelegateFlowLayout{
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.register(HomeCell.self, forCellWithReuseIdentifier: homeCellId)
collectionView?.register(TrendingCell.self, forCellWithReuseIdentifier: trendingCellId)
collectionView?.register(SubscriptionCell.self, forCellWithReuseIdentifier: subscriptionCellId)
collectionView?.register(AccountCell.self, forCellWithReuseIdentifier: accountCellId)
}
}Run Code Online (Sandbox Code Playgroud)
让我们以 HomeController 的第一个单元(称为 HomeCell)来说明我的问题。Homecell 具有三个自定义单元,称为 VideoCell、CategoryCell 和 UserSearchCell。
class HomeCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
let cellId = "cellId"
let searchId = "sarchId"
let scrollId = "scrollId"
lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.white
cv.dataSource = …Run Code Online (Sandbox Code Playgroud)uinavigationcontroller ios uicollectionviewcell uicollectionviewdelegate swift