Best way to refresh/reload UIScrollView

前端 未结 6 1976
走了就别回头了
走了就别回头了 2021-01-04 05:59

I have a method that adds some subviews (images/buttons) to my ScrollView.

The thing is that from outside of the ScrollView the user can tap a button that will chang

相关标签:
6条回答
  • 2021-01-04 06:05

    Upon removing any unneeded views (which I'm assuming you're not going to be able to work around) call [myScrollView setNeedsDisplay]; to refresh the UIScrollView on the next drawing cycle.

    0 讨论(0)
  • I generally keep a separate mutable array of views that I've put into the scrollview, and iterate over that instead of subviews. That way I only take out what I put in. But the basic approach is the same.

    0 讨论(0)
  • 2021-01-04 06:10

    Here is what I did to remove all subview except scrollbar, I removed all subviews that have frame.size.width greater than 10, scrollbar width is smaller than 10, so it will not be removed. This assumes that all the views that you add to UIScollView have a width greater than 10.

    for (UIView * view in view.subviews) {
        if(view.frame.size.width > 10){ // removes all component except scrollbar
            [view removeFromSuperview];
        }
    }
    
    0 讨论(0)
  • 2021-01-04 06:16

    This is kind of an old thread, but a slightly easier method might just be to add a tag to the view's your adding to the UIScrollView, let's say 15. Then

    for (UIView * view in _scrollView.subviews) {
        if (view.tag == 15) {
            [view removeFromSuperview];
        }
    }
    

    this way you can be sure that only the view's you have added get removed. Just a thought.

    0 讨论(0)
  • 2021-01-04 06:22

    You have to set this lines before start scroll method, it's simply remove all data from scrollview and as per your method scrollview will reset.

           NSArray* subviews = [[NSArray alloc] initWithArray: Main_Scroll.subviews];
            for (UIView* view in subviews)
            {
                if ([view isKindOfClass:[UIView class]])
                {
                    [view removeFromSuperview];
                }
                if ([view isKindOfClass:[UIImageView class]])
                {
                    [view removeFromSuperview];
                }
                if ([view isKindOfClass:[UIButton class]])
                {
                    [view removeFromSuperview];
                }
                if ([view isKindOfClass:[UILabel class]])
                {
                    [view removeFromSuperview];
                }
            }
    
    0 讨论(0)
  • 2021-01-04 06:31

    Calling setNeedsDisplay is just going to redraw your scroll view, not actually remove the content inside of it.

    As you've discovered, calling removeFromSuperview on all scroll view subviews will result in your scroll bars sometimes being removed as well (not great).

    There are a couple of approaches around this: the first is to maintain a distinct array containing all the views you yourself have added to the scroll view. You can then just iterate through this array instead. In my view this is probably the best approach.

    The other, slightly hacky, way is to test the views as you iterate through them:

    for (UIView *view in self.contentView.subviews)
    {
        if (![view isKindOfClass:[UIImageView class]])
            [view removeFromSuperview];
    }
    

    Because the scroll bars are themselves a UIImageView subclass this will not remove them. But then again, if you're using image views yourself they won't get removed either, and there's no guarantee in future iOS versions they will remain image views.

    So much better to go with my first approach and just keep an array around with all the views you've added.

    0 讨论(0)
提交回复
热议问题