how to remove subviews from scrollview?

后端 未结 8 2235
时光取名叫无心
时光取名叫无心 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:16

    The problem with the UIScrollView and others subclass of UIView is that they contains initially some views (like the vertical and horizontal scrollbar for the UIScrollView). So i created a category of UIView to delete the Subviews filtered on the class.

    For example:

    [UIScrollView removeAllSubviewsOfClass:[FooView class],[BarView class],nil];
    

    The code:

    - (void)removeAllSubviewsOfClass:(Class)firstClass, ... NS_REQUIRES_NIL_TERMINATION;
    
    
    - (void)removeAllSubviewsOfClass:(Class)firstClass, ...
    {
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"FALSEPREDICATE"];
    
        va_list args;
        va_start(args, firstClass);
    
        for (Class class = firstClass; class != nil; class = va_arg(args, Class)) 
        {
            predicate = [NSCompoundPredicate orPredicateWithSubpredicates:[NSArray arrayWithObjects:predicate,[NSPredicate predicateWithFormat:@"self isKindOfClass:%@",class], nil]];
        }
    
        va_end(args);
        [[self.subviews filteredArrayUsingPredicate:predicate] makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    }
    

提交回复
热议问题