【问题标题】:Create Vertical lines between Uilabels inside of a stack view [duplicate]在堆栈视图内的 Uilabels 之间创建垂直线 [重复]
【发布时间】: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,但无法在标签之间添加此视图。谁能帮助我。谢谢

【问题讨论】:

标签: ios swift user-interface uistackview


【解决方案1】:

如果您在界面生成器中执行此操作,那将非常容易。将标签添加到水平堆栈视图,然后在其间添加分隔视图,为分隔视图创建 1 点宽度约束,将它们的拥抱优先级设置为高于标签并将堆栈视图的对齐设置为 fill 并将分布设置为 equal spacing .就是这样。您可能还想将分隔线的宽度约束优先级设置为 999,这样 xcode 在某些情况下就不会抱怨破坏约束。

这是我的快速测试的屏幕截图:

以及模拟器中的结果:

我的示例是直接在视图控制器的视图中完成的,但是您当然可以使用 xib 文件创建一个新的 UIView 子类,将标签(和分隔符)放在那里,然后实例化您的视图,如下所示:@ 987654327@。然后只需将您的视图实例添加到您的集合视图 cll 作为子视图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    • 1970-01-01
    相关资源
    最近更新 更多