iOS 7 UIRefreshControl tintColor not working for beginRefreshing

前端 未结 19 1585
我在风中等你
我在风中等你 2020-12-04 13:48

I\'m trying to set a tintColor on my UIRefreshControl (building on iOS 7). I enabled refreshing for the tableViewController in storyboard, then in my ViewController vi

相关标签:
19条回答
  • 2020-12-04 14:29

    Hey just stumbled into this exact issue.

    Interestingly I fixed my code by setting the contentOffset first then calling beginRefreshing

    if(self.tableView.contentOffset.y == 0){
        self.tableView.contentOffset = CGPointMake(0, -self.refreshControl.frame.size.height);
        [self.refreshControl beginRefreshing];
    }
    

    You may want to animate this process:

    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^(void){
        self.tableView.contentOffset = CGPointMake(0, -self.refreshControl.frame.size.height);
    } completion:^(BOOL finished) {
        [self.refreshControl beginRefreshing];
    }];
    

    Hope this helps you.

    W

    0 讨论(0)
  • 2020-12-04 14:29

    Try setting the tintColor of your UIRefreshControl in viewWillAppear.

    0 讨论(0)
  • 2020-12-04 14:31

    I develop for iOS using Xamarin (C#) and came across the same issue.

    I fixed the coloring issue, by setting the AttributedTitle of the RefreshControl :

    private CGPoint originalOffset;
    ...
    public override void ViewDidLoad ()
    {
         base.ViewDidLoad ();
         ...
         originalOffset = TableView.ContentOffset; // Store the original offset of the table view
         RefreshControl = new UIRefreshControl (){ TintColor = UIColor.Red };
         RefreshControl.ValueChanged += ((s,e) => { Update (this, EventArgs.Empty); });
         // Hack so the TintColor of the RefreshControl will be properly set
         RefreshControl.AttributedTitle = new NSAttributedString ("Fetching data");
    }
    

    My Update method looks like this :

    private async void Update(object sender, EventArgs args)
    {
         try {
              TableView.UserInteractionEnabled = false;
              // I find -100 to be a big enough offset
              TableView.SetContentOffset (new CGPoint (0, -100), true);
              RefreshControl.BeginRefreshing ();
              ... // Fetch data & update table source 
              TableView.ReloadData ();
          } catch(Exception) {
              // Respond to exception
          } finally {
              // Put the offset back to the original
              TableView.SetContentOffset (originalOffset, true);
              RefreshControl.EndRefreshing ();
              TableView.UserInteractionEnabled = true;
          }
    }
    

    Once the ViewDidAppear, I call Update programmatically. Before setting the attributed title, my spinner would've been black. Now it has the proper red color.

    It's worth noticing, that this 'hack/fix' also comes with a second bug. The first time you refresh, you'll notice that the AttributedTitle is not displayed. Refreshing a second (,third,fourth,...) time will display the title properly. But if you don't want a title, you just initialize it with an empty string, and this is not a big issue to you.

    I hope this can be of use to others.

    0 讨论(0)
  • 2020-12-04 14:33

    SWIFT:

    I am using Swift and > iOS8. Most of the described workarounds didn't work for me. That's how I got it working:

    In viewDidLoad:

    customRefreshControl.tintColor = UIColor.clearColor()
    

    The following doesn't have to be inside viewDidLoad. I put it in an extra function which get's called every time I update the tableView:

    private func startRefreshControlAnimation() {
    
        self.tableView.setContentOffset(CGPointMake(0, -self.customRefreshControl.frame.size.height), animated: true)
    
        CATransaction.begin()
        self.customRefreshControl.beginRefreshing()
        CATransaction.commit()
    
    }
    
    0 讨论(0)
  • 2020-12-04 14:35

    this hack is very working

    var refreshWasProgramBeginning: Bool = false
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    
        if !refreshWasProgramBeginning {
            UIView.animate(withDuration: 0.25, animations: {
                self.tableView.contentOffset = CGPoint.init(x: 0, y: -self.refreshControl.frame.height)
            }) { (_) in
                self.refreshControl.beginRefreshing()
                self.refreshWasProgramBeginning = true
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-04 14:36

    Add an extension for UIResfreshControl.

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