Skip to content

Instantly share code, notes, and snippets.

@lamprosg
Created October 18, 2019 12:46
Show Gist options
  • Save lamprosg/6b43f9d1b874bd1b22e11267fecb972c to your computer and use it in GitHub Desktop.
Save lamprosg/6b43f9d1b874bd1b22e11267fecb972c to your computer and use it in GitHub Desktop.
(iOS) StackView
private func renderData() {
let subviews = (0..<data.count).map { (index: Int) -> Someview in
let nib = UINib(nibName: "Someview", bundle: nil)
let viewArray = nib.instantiate(withOwner: nil, options: nil)
guard let view = viewArray.first as? Someview else {
return Someview()
}
view.titleLabel.text = Array(data[index].keys).first
view.valueLabel.text = Array(data[index].values).last
return view
}
subviews.forEach { (view) in
stackView.addArrangedSubview(view)
}
}
extension UIStackView {
func removeAllArrangedSubviews() {
//Get all subviews
let removedSubviews = arrangedSubviews.reduce([]) { (allSubviews, subview) -> [UIView] in
self.removeArrangedSubview(subview)
return allSubviews + [subview]
}
// Deactivate all constraints
NSLayoutConstraint.deactivate(removedSubviews.flatMap({ $0.constraints }))
// Remove the views from self
removedSubviews.forEach({ $0.removeFromSuperview() })
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment