UIRefreshControl's tintColor can't be initially set

311 Views Asked by At

I'm currently struggling with a problem that I couldn't handle and I wasn't able to find any answer or workarounds.

The problem is that I want to change color for the refreshControl to .white for example, I call the beginRefreshingManually() function in every viewcontroller's viewDidLoad() but the initial color is still black. When trying to manually pull to refresh the Refreshcontrol becomes .white

Here's some of the code I've wrote:

extension UIScrollView {
    func setupRefreshControl(withTintColor tintColor: UIColor = JDColorConstants.UIScrollView.refreshControlWhiteColor.color,
                             completion: @escaping (() -> Void)) {
        let refreshControl = UIRefreshControl()
        refreshControl.tintColor = tintColor
        refreshControl.addAction(forEvent: .valueChanged, closure: {
            DispatchQueue.main.asyncAfter(deadline: .now() + JDDurationConstants.shortAnimationDuration) {
                completion()
            }
        })

        self.refreshControl = refreshControl
    }
}

extension UIRefreshControl {
    func beginRefreshingManually() {
        DispatchQueue.main.async { [weak self] in
            guard let scrollView = UIApplication.shared.topViewController()?.getScrollView() else { return }
            let constant = UIApplication.shared.getTopHeightOfSafeArea() +
                (UIApplication.shared.topViewController()?.navigationController?.navigationBar.bounds.height ?? 0) +
                (self?.frame.height ?? 0)
            scrollView.setContentOffset(CGPoint(x: 0, y: -constant), animated: true)
            self?.sendActions(for: .valueChanged)
            self?.beginRefreshing()
        }
    }
}
1

There are 1 best solutions below

0
cimicimi On BEST ANSWER

Did you try call manually beginRefreshingManuall method in viewDidLoad ?

override func viewDidLoad() {
        super.viewDidLoad()
        //it will react as if you were being renewed with your hand
        yourUIRefreshControl.beginRefreshingManually()
    }