iOS - Add vertical line programatically inside a stack view

后端 未结 10 867
悲哀的现实
悲哀的现实 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:32

    I've upvoted @FrankByte.com's answer because he helped me get to my solution, which is pretty similar to what the OP wanted to do:

    
    extension UIStackView {
        func addVerticalSeparators(color : UIColor, multiplier: CGFloat = 0.5) {
            var i = self.arrangedSubviews.count - 1
            while i > 0 {
                let separator = createSeparator(color: color)
                insertArrangedSubview(separator, at: i)
                separator.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: multiplier).isActive = true
                i -= 1
            }
        }
    
        private func createSeparator(color: UIColor) -> UIView {
            let separator = UIView()
            separator.widthAnchor.constraint(equalToConstant: 1).isActive = true
            separator.backgroundColor = color
            return separator
        }
    }
    

提交回复
热议问题