我想在Code中创建一个带有自定义标头的UICollectionView。因此,我创建了一个UICollectionViewCell的子类来描述我的自定义标题。我想在标题中以水平线显示五个标签。因此我创建了五个标签并将它们添加到stackView中。 stackview显示我的标签填写相同。迄今为止一切都很完美
Label1 Label2 Label3 Label4 Label5
//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 = .horizontal
stackView.distribution = .fillEqually
addSubview(stackView)
// add stackView to my view
stackView.topAnchor.constraint(equalTo: topAnchor).isActive = true
stackView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
stackView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
stackView.heightAnchor.constraint(equalToConstant: 50).isActive = true
现在我想在这些标签之间添加单独的垂直线:
Label1 | Label2 | Label3 | Label4 | Label5
我创建了一个UIView,但无法在标签之间添加此视图。任何人都可以帮助我。感谢
答案 0 :(得分:4)