iOS - Add vertical line programatically inside a stack view

后端 未结 10 875
悲哀的现实
悲哀的现实 2020-12-25 09:04

I\'m trying to add vertical lines, between labels inside a stack view all programatically.

The desired finish will be something like this image:

I c

10条回答
  •  遥遥无期
    2020-12-25 09:35

    @GOR answer extension only for vertical line and only in centers

    Stackview settings: set width contraints of each subview and parent stackview should be fill

    Here's a simple extension for adding vertical separators between each row.

    func addVerticalSeparators(color : UIColor) {
        var i = self.arrangedSubviews.count
        while i > 1 {
            let separator = verticalCreateSeparator(color: color)
            insertArrangedSubview(separator, at: i-1)   // (i-1) for centers only
            separator.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: 1).isActive = true
            i -= 1
        }
    }
    
    private func verticalCreateSeparator(color : UIColor) -> UIView {
        let separator = UIView()
        separator.widthAnchor.constraint(equalToConstant: 1).isActive = true
        separator.backgroundColor = color
        return separator
    } 
    

提交回复
热议问题