Created
October 18, 2019 12:46
-
-
Save lamprosg/6b43f9d1b874bd1b22e11267fecb972c to your computer and use it in GitHub Desktop.
(iOS) StackView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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