Access nested stack views

2019-08-02 10:53发布

问题:

Obviously overseeing something simple. Need to access a nested stack view in another stack view.

If I wanted to set spacing to all nested stackViews for passenger 2 how would I do that?

// Select passenger 2
let tag = 2

// Select wrapper stackView for passenger 2
let stackView = self.view.viewWithTag(tag) as! UIStackView!

let nestedStackViews = stackView. <-- WHAT GOES HERE??

for sv in nestedStackViews{
    sv?.spacing = 0 // Set nested stackView spacing to 0 to avoid constraint issues
}

To Visualize (need to set spacing of firstName_P2, surname_P2 and email_P2 to 0)

SOLUTION: Based on Winter's answer

Providing more detail for others who might be struggling, make sure to convert the subview to a stackview

// Select passenger 2
let tag = 2

// Select wrapper stackView for passenger 2
let stackView = self.view.viewWithTag(tag) as! UIStackView!

if let nestedStackViews = stackView?.subviews{
    for sv in nestedStackViews{
        let nsv = sv as! UIStackView
        nsv.spacing = 0
    }
}

回答1:

Use subviews: stackView.subviews