UIRefreshControl - beginRefreshing not working when UITableViewController is inside UINavigationController

后端 未结 15 769
梦如初夏
梦如初夏 2020-11-28 03:03

I\'ve setup a UIRefreshControl in my UITableViewController (which is inside a UINavigationController) and it works as expected (i.e. pull down fires the correct event). Howe

相关标签:
15条回答
  • 2020-11-28 03:53

    Here is Swift 3 and later extension that shows spinner as well as animate it.

    import UIKit
    extension UIRefreshControl {
    
    func beginRefreshingWithAnimation() {
    
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {
    
            if let scrollView = self.superview as? UIScrollView {
                scrollView.setContentOffset(CGPoint(x: 0, y: scrollView.contentOffset.y - self.frame.height), animated: true)
              }
            self.beginRefreshing()
          }
       }
    }
    
    0 讨论(0)
  • 2020-11-28 03:55

    For Swift 5, for me the only thing missing was to call refreshControl.sendActions(.valueChanged). I made an extension to make it more cleaner.

    extension UIRefreshControl {
    
        func beginRefreshingManually() {
            if let scrollView = superview as? UIScrollView {
                scrollView.setContentOffset(CGPoint(x: 0, y: scrollView.contentOffset.y - frame.height), animated: false)
            }
            beginRefreshing()
            sendActions(for: .valueChanged)
        }
    
    }
    
    0 讨论(0)
  • 2020-11-28 03:55

    I use the same technique for show user "data is update" visual sign. A result user bring app from background and feeds/lists will be update with UI like users pull tables to refresh himself. My version contain 3 things

    1) Who send "wake up"

    - (void)applicationDidBecomeActive:(UIApplication *)application {
        [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationHaveToResetAllPages object:nil];
    }
    

    2) Observer in UIViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(forceUpdateData) name:kNotificationHaveToWakeUp:nil];
    }
    

    3)The protocol

    #pragma mark - ForcedDataUpdateProtocol
    
    - (void)forceUpdateData {
        self.tableView.contentOffset = CGPointZero;
    
        if (self.refreshControl) {
            [self.refreshControl beginRefreshing];
            [self.tableView setContentOffset:CGPointMake(0, -self.refreshControl.frame.size.height) animated:YES];
            [self.refreshControl performSelector:@selector(endRefreshing) withObject:nil afterDelay:1];
        }
    }
    

    0 讨论(0)
  • 2020-11-28 03:57

    It's works perfect to me:

    Swift 3:

    self.tableView.setContentOffset(CGPoint(x: 0, y: -self.refreshControl!.frame.size.height - self.topLayoutGuide.length), animated: true)
    
    0 讨论(0)
  • 2020-11-28 04:00

    tested on Swift 5

    use this in viewDidLoad()

    fileprivate func showRefreshLoader() {
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {
            self.tableView.setContentOffset(CGPoint(x: 0, y: self.tableView.contentOffset.y - (self.refreshControl.frame.size.height)), animated: true)
            self.refreshControl.beginRefreshing() 
        }
    }
    
    0 讨论(0)
  • 2020-11-28 04:03

    Here's a Swift extension using the strategies described above.

    extension UIRefreshControl {
        func beginRefreshingManually() {
            if let scrollView = superview as? UIScrollView {
                scrollView.setContentOffset(CGPoint(x: 0, y: scrollView.contentOffset.y - frame.height), animated: true)
            }
            beginRefreshing()
        }
    }
    
    0 讨论(0)
提交回复
热议问题