【发布时间】:2017-04-24 19:39:52
【问题描述】:
我想在代码中创建一个带有自定义标题的 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 = .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
现在我想在这些标签之间添加单独的垂直线,如下所示:
标签1 |标签2 |标签3 |标签4 |标签5
我创建了一个 UIView,但无法在标签之间添加此视图。谁能帮助我。谢谢
【问题讨论】:
-
我看不到任何将分隔符添加到 StackView 的代码
-
谢谢布兰登。所以这意味着 UIstackview 是一个非渲染视图,它不能完成。
标签: ios swift user-interface uistackview