如何在 Swift 中刪除視圖的所有子視圖? - How to remove all subviews of a view in Swift?

問題:

I'm looking for a simple method to remove at once all subviews from a superview instead of removing them one by one.我正在尋找一種簡單的方法來一次從超級視圖中刪除所有子視圖,而不是一個一個地刪除它們。

//I'm trying something like this, but is not working
let theSubviews : Array = container_view.subviews
for (view : NSView) in theSubviews {
    view.removeFromSuperview(container_view)
}

What I am missing?我缺少什麼?

UPDATE更新

My app has a main container_view .我的應用程序有一個主container_view I have to add different other views as subviews to container_view in order to provide a sort of navigation.我必須將不同的其他視圖作爲子視圖添加到container_view以提供一種導航。

So, when clicking the button to "open" a particular page, I need to remove allsubviews and add the new one.因此,當單擊按鈕“打開”特定頁面時,我需要刪除所有子視圖並添加新的子視圖。

UPDATE 2 - A working solution (OS X)更新 2 - 一個有效的解決方案 (OS X)

I guess Apple fixed it.我猜蘋果修復了它。

Now it is more easy than ever, just call:現在比以往任何時候都更容易,只需調用:

for view in containerView.subviews{
    view.removeFromSuperview()
}

解決方案:

參考一: https://en.stackoom.com/question/1e0rI
參考二: https://stackoom.com/question/1e0rI
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章