UIRefreshControl bug when entering foreground

前端 未结 3 1765
小蘑菇
小蘑菇 2021-02-19 16:49

I\'ve noticed a little bug (but really annoying) when I use UIRefreshControl in my View Controller. When the application returns from the background the UIRe

3条回答
  •  臣服心动
    2021-02-19 17:15

    I found one workaround for a variation of this bug. It might potentially help with yours, too. In my app, switching to another tab and going back to the tab with the refresh control broke it - the control didn't animate any more while dragging.

    Instead of setting up UIRefreshControl in viewDidLoad, I set it up in viewDidAppear, and then always remove it in viewDidDisappear. This way it's always initialised fresh and doesn't get confused.

    As in the previous answer to this question, i've requested UIApplicationDidBecomeActiveNotification so that the refresh control can be fixed also when the user jumps back in the app.

    @implementation MYViewController {
        UIRefreshControl *refreshControl;
    }
    
    - (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
    
        /* Pull down to refresh. iOS bug: If we add this in viewDidLoad and let it
         * stay there for the lifetime of the view, the control will misbehave
         * after going to another view/tab and coming back (will not animate nicely
         * on drag).
         */
        [self setupRefreshControl];
    
        /* We'll want to be notified for didBecomeActive, to do the pull-down-to-refresh workaround when resuming. */
        [[NSNotificationCenter defaultCenter] addObserver:self
            selector:@selector(setupRefreshControl)
                name:UIApplicationDidBecomeActiveNotification object:nil];
    }
    
    - (void)setupRefeshControl
    {
        if (refreshControl)
            [refreshControl removeFromSuperview];
    
        refreshControl = [[UIRefreshControl alloc] init];
        [refreshControl addTarget:self action:@selector(refreshPull:)
            forControlEvents:UIControlEventValueChanged];
    
        [scrollView addSubview:refreshControl];
    }
    
    - (void)viewDidDisappear:(BOOL)animated
    {
        [super viewDidDisappear:animated];
    
        [refreshControl removeFromSuperview];
        refreshControl = nil;
    
        /* We don't need notification for becoming active any more */
        [[NSNotificationCenter defaultCenter] removeObserver:self
            name:UIApplicationDidBecomeActiveNotification
          object:nil];
    }
    

    Slightly suboptimal, but works.

提交回复
热议问题