iOS 7 UIRefreshControl tintColor not working for beginRefreshing

前端 未结 19 1584
我在风中等你
我在风中等你 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:39

    SWIFT SOLUTION ! Insert the following code in the viewDidLoad:

    self.refreshControl.tintColor = UIColor.orangeColor()
    self.tableView.contentOffset = CGPointMake(0, -self.refreshControl.frame.size.height)
    self.refreshControl.beginRefreshing()
    

    Swift 3.1

    self.refreshControl.tintColor = UIColor.orange
    self.tableView.contentOffset = CGPoint(x:0, y:-self.refreshControl.frame.size.height)
    self.refreshControl.beginRefreshing()
    
    0 讨论(0)
  • 2020-12-04 14:39

    None of these answers are working for me correctly on iOS8, with the closest being @jpsim's answer but that still left an unsightly black refresh control during its fade-in animation (it would cross-fade between black and while over the course of the animation).

    The solution that worked for me was to put this immediately after creating the refresh control in my viewDidLoad:

    self.refreshControl = [[UIRefreshControl alloc] init];
    self.refreshControl.tintColor = [UIColor whiteColor];
    ...
    self.refreshControlHeight = self.refreshControl.frame.size.height;
    [self.tableView setContentOffset:CGPointMake(0, -1) animated:NO];
    [self.tableView setContentOffset:CGPointMake(0, 0) animated:NO];
    

    Then to show the UIRefreshControl programmatically:

    [self.tableView setContentOffset:CGPointMake(0, self.tableView.contentOffset.y-self.refreshControlHeight) animated:YES];
    [self.refreshControl beginRefreshing];
    

    I had to store the height of the refresh control, as while it was set for the first invocation, subsequent calls would have a 0 height.

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

    Force the setTintColor to run in the main thread. (Main thread updates the ui).

    [[NSOperationQueue mainQueue] addOperationWithBlock:^ {
        [self.refreshControl setTintColor:[UIColor redColor]];
        [self.refreshControl beginRefreshing];
    }];
    
    0 讨论(0)
  • 2020-12-04 14:42

    I created a drop-in UIRefreshControl+beginRefreshing category that fixes this issue.

    In brief, it fixes tintColor issue and manually tableView adjust contentOffset to make sure the refresh control is visible. Please try :)

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

    This is a bug which occurs when calling beginRefreshing() on the refresh control right after setting its tintColor property (or calling it from viewDidLoad() (details here). There is an easy workaround however, by wrapping the beginRefreshing() call inside a defer statement (Swift 4):

    override func viewDidLoad() {
        super.viewDidLoad()
        refreshControl.tintColor = .red
        defer {
            refreshControl.beginRefreshing()
        }
    }
    
    0 讨论(0)
  • 2020-12-04 14:47

    I combined some of the previous answers. This works for me on iOS 9 and Swift 2:

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
    
        let contentOffset = self.tableView.contentOffset.y
        UIView.animateWithDuration(0, delay: 0, options: .BeginFromCurrentState, animations: {
            print(self.tableView.contentOffset.y)
                self.tableView.setContentOffset(CGPointMake(0, -self.refreshControl.frame.size.height), animated: false)
            }, completion: { finished in
                self.refreshControl.beginRefreshing()
                self.tableView.setContentOffset(CGPointMake(0, contentOffset/2-self.refreshControl.frame.size.height), animated: true)
                self.refresh() // Code that refresh table data
        })        
    }
    
    0 讨论(0)
提交回复
热议问题