I have UIStackView in vertical mode filled with UIButtons. I have dynamic screen resize and if all buttons in stack view have height under some threshold, I want to hide them. How to achive this automatically?
I have tried to extend UIButton and add:
override func layoutSubviews() {
super.layoutSubviews()
self.isHidden = (self.frame.height < 20)
}
which works, but once the button is hidden it will never re-appear and layoutSubviews is never called back (even if the height should be again larger).
It's not clear what all you are doing, or why you say it would be problematic to set the buttons'
.alphaproperty, but here are two approaches, both using aUIStackViewsubclass and handling the show/hide inlayoutSubviews().1: calculate what the button heights will be and set
.isHiddenproperty:set
.isHiddenproperty based on what the button heights are (much simpler):class MyStackView: UIStackView {
}
And here's a sample controller to see it in use. Tapping anywhere will toggle the height of the stack view between
300and100(buttons will have less-than 20-pts height at100):