UIRefreshControl tint color doesn't match given color

别等时光非礼了梦想. 提交于 2019-12-08 16:19:24

问题


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]}];


回答1:


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.




回答2:


UIRefreshControl is a buggy class. I noticed that placing the tvc.refreshControl.tintColor = [UIColor colorWithHex:0x2C76BE]; inside an animation block (even of zero duration) would yield the expected the results. So I tested to do this hideous 'hack': dispatch_async(mainQueue, <#set tintColor#>); and that also give the right result. There might also be a dependency of the refreshcontrol on the the timing of calling -beginRefreshing or -endRefreshing too.

Because I was annoyed so much by UIRefreshControl's buggyness and limitation of only being usable in a UITableViewController, I created a fully customizable one of my own, usable with any type of UIScrollView (UICollectionView, UITableView). Note that I created this before UICollectionViewFlowLayout supported the sticky headers like a tableView, so my refreshcontrol does not work well when that option is on. Feel free to submit a fix ;).

You can find it here https://github.com/Joride/JRTRefreshControl (if this falls under the 'shameless plugging clause' I will remove this link, but I think it is relevant to the question.



来源:https://stackoverflow.com/questions/32744522/uirefreshcontrol-tint-color-doesnt-match-given-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!