how to remove subviews from scrollview?

后端 未结 8 2138
时光取名叫无心
时光取名叫无心 2020-12-02 09:10

how do i remove all subviews from my scrollview...

i have a uiview and a button above it in the scrollview something like this....

here is my code to add sub

相关标签:
8条回答
  • 2020-12-02 09:35

    To remove all the subviews from any view, you can iterate over the subviews and send each a removeFromSuperview call:

    // With some valid UIView *view:
    for(UIView *subview in [view subviews]) {
        [subview removeFromSuperview];
    }

    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:

    • 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:
    // Again, valid UIView *view:
    for(UIView *subview in [view subviews]) {
        if([subview isKindOfClass:[UIButton class]]) {
            [subview removeFromSuperview];
        } else {
            // Do nothing - not a UIButton or subclass instance
        }
    }
    0 讨论(0)
  • 2020-12-02 09:36

    The easiest and Best way is

     for(UIView *subview in [scrollView subviews]) {
    
         [subview removeFromSuperview];
    
     }
    
    0 讨论(0)
提交回复
热议问题