Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIRefreshControl tint color doesn't match given color

The refresh color doesn't match the tint color and looks different, i tryied to change tintAdjustmentMode but the result is the same

Just to note, the spinner and text color should be 0x2C76BE

tvc.refreshControl = [UIRefreshControl new];
tvc.refreshControl.tintAdjustmentMode = UIViewTintAdjustmentModeNormal;
tvc.refreshControl.tintColor = [UIColor colorWithHex:0x2C76BE];
tvc.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to query spectrum again" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHex:0x2C76BE]}];

enter image description here

like image 571
Peter Lapisu Avatar asked Sep 23 '15 16:09

Peter Lapisu


1 Answers

I had a similar problem with UIRefreshControl not displaying the color correctly when the view loads and I call beginRefreshing(). If the user pulls to refresh, the control correctly displays the tintColor I've specified.

First, subclass the refresh control. Then, override the didMoveToWindow method in the subclass. The following code finds the element that's animated to create the spinner and sets its background color.

This code uses an extension to UIView to return all the view's subviews (I used Jon Willis' answer from Swift: Recursively cycle through all subviews to find a specific class and append to an array).

class CustomRefreshControl: UIRefreshControl {

    override func didMoveToWindow() {
        super.didMoveToWindow()
        if let l = getNestedSubviews().first(where: { $0.layer is CAReplicatorLayer }), l.subviews.count > 0 {
            l.subviews[0].backgroundColor = UIColor.orange //getNestedSubviews method is an extension of UIView as referenced above
        }
}

The spinner has a CAReplicatorLayer whose view contains one subview. That subview is simply a rectangle implements the graphic element for the spinner. It's that graphic element you're coloring.

like image 130
slucas Avatar answered Sep 27 '22 18:09

slucas