Skip to content

Instantly share code, notes, and snippets.

@nil-biribiri
Created January 25, 2019 04:54
Show Gist options
  • Save nil-biribiri/f1996ec544b0878edfcd3801ef3c82ab to your computer and use it in GitHub Desktop.
Save nil-biribiri/f1996ec544b0878edfcd3801ef3c82ab to your computer and use it in GitHub Desktop.
Extension for insert separators in UIStackView
import UIKit
extension UIStackView {
func addSeparator(color: UIColor, withThickness thickness: CGFloat = 1.0 ) {
var index = self.arrangedSubviews.count
while index > 1 {
let separator = createSeparator(color: color, withThickness: thickness)
insertArrangedSubview(separator, at: index - 1) // (index-1) for centers only
if self.axis == .horizontal {
separator.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: 1).isActive = true
} else {
separator.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 1).isActive = true
}
index -= 1
}
}
private func createSeparator(color : UIColor, withThickness thickness: CGFloat) -> UIView {
let separator = UIView()
if self.axis == .horizontal {
separator.widthAnchor.constraint(equalToConstant: thickness).isActive = true
} else {
separator.heightAnchor.constraint(equalToConstant: thickness).isActive = true
}
separator.backgroundColor = color
return separator
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment