访问嵌套堆栈视图

时间:2017-02-24 01:34:18

标签: swift swift3 uistackview

显然是在监督简单的事情。需要访问另一个堆栈视图中的嵌套堆栈视图。

如果我想为乘客2的所有嵌套stackView设置间距,我该怎么做?

// 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
}

要可视化(需要将firstName_P2,surname_P2和email_P2的间距设置为0) enter image description here

解决方案:根据Winter的回答

为可能苦苦挣扎的其他人提供更多详细信息,请务必将子视图转换为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 个答案:

答案 0 :(得分:4)

使用subviewsstackView.subviews