I have my setup as followed:
UIView(1) -> UIScrollview -> UIStackview -> UIViews(2) -> UIButton
How to loop through every UIButton in all the UIViews(2) in the UIStackview? I got more buttons in my UIView(1) so I can not loop through every button in the whole Viewcontroller. I tried:
for view in self.stackview.subviews as [UIView] {
if let btn = view as? UIButton {
print("Worked")
}
}
for view in self.scrollview.subviews as [UIView] {
if let btn = view as? UIButton {
print("Worked")
}
}
for case let button as UIButton in self.scrollview.subviews {
print("worked")
}
for case let button as UIButton in self.stackview.subviews {
print("worked")
}
It just don't prints anything when trying to add this in my viewDidLoad function. What am I doing wrong here?
The first solution that comes in mind:
let buttons = self.stackview.subviews // direct subviews of stack view
.map { subview in subview.subviews } // second level subviews
.joined() // join the second level subviews into one flat array
.compactMap { $0 as? UIButton } // filter buttons
Basically you are only forgetting to jump to the second level. You are iterating only the first level subviews.
If you want a more stable solution, you can iterate recursively over the subviews in any depth, e.g. using a category:
extension UIView {
var allSubviews: [UIView] {
return self.subviews + self.subviews.map { $0.allSubviews }.joined()
}
}
and then
let buttons = self.stackview.allSubviews.compactMap { $0 as? UIButton }
How about…
for view in stackView.subviews {
for case let button as UIButton in view.subviews {
print(button.title(for: .normal)) // Do something with button
}
}
or
for case let button as UIButton in stackView.subviews.flatMap({ $0.subviews }) {
print(button.title(for: .normal)) // Do something with button
}
or
for button in stackView.subviews.flatMap({ $0.subviews }).flatMap({ $0 as? UIButton }) {
print(button.title(for: .normal)) // Do something with button
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With