无法初步设置UIRefreshControl的tintColor

时间:2019-11-25 13:53:28

标签: swift scrollview ios13 uirefreshcontrol tintcolor

我目前正在努力解决一个无法解决的问题,并且找不到任何答案或解决方法。

问题是,例如,我想将refreshControl的颜色更改为.white,我在每个视图控制器的beginRefreshingManually()中调用了viewDidLoad()函数,但是初始颜色仍然是黑色。尝试手动拉动刷新时,Refreshcontrol变为.white

以下是我编写的一些代码:

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 个答案:

答案 0 :(得分:0)

您是否尝试在viewDidLoad中手动调用beginRefreshingManuall方法?

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