問題描述
如何從我的滾動視圖中刪除所有子視圖...
how do i remove all subviews from my scrollview...
我在滾動視圖中有一個 uiview 和它上方的按鈕,如下所示....
i have a uiview and a button above it in the scrollview something like this....
這是我在滾動視圖中添加子視圖的代碼
here is my code to add subview in scroll view
這是刪除子視圖的代碼
推薦答案
要從任何視圖中刪除所有子視圖,您可以遍歷子視圖并為每個子視圖發(fā)送 removeFromSuperview
調(diào)用:
To remove all the subviews from any view, you can iterate over the subviews and send each a removeFromSuperview
call:
不過,這完全是無條件的,并且會刪除給定視圖中的所有子視圖.如果你想要更細粒度的東西,你可以采取幾種不同的方法:
This is entirely unconditional, though, and will get rid of all subviews in the given view. If you want something more fine-grained, you could take any of several different approaches:
- 維護您自己的不同類型的視圖數(shù)組,以便稍后以相同的方式向它們發(fā)送
removeFromSuperview
消息 - 保留您創(chuàng)建它們的所有視圖并保留指向這些視圖的指針,以便您可以根據(jù)需要單獨發(fā)送它們
removeFromSuperview
- 在上述循環(huán)中添加
if
語句,檢查類是否相等.例如,要僅刪除視圖中存在的所有 UIButton(或 UIButton 的自定義子類),您可以使用以下內(nèi)容:
- Maintain your own arrays of views of different types so you can send them
removeFromSuperview
messages later in the same manner - Retain all your views where you create them and hold on to pointers to those views, so you can send them
removeFromSuperview
individually as necessary - Add an
if
statement to the above loop, checking for class equality. For example, to only remove all the UIButtons (or custom subclasses of UIButton) that exist in a view, you could use something like:
這篇關(guān)于如何從滾動視圖中刪除子視圖?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!