我编写了一个扩展,允许我在UIViewController中的常规tableView上显示refreshControl。下面的代码在viewDidLoad()和pull to refresh上工作得很好。
分机:

extension UITableView
{
    func findRefreshControl () -> UIRefreshControl?
    {
        for view in subviews
        {
            if view is UIRefreshControl
            {
                print("The refresh control: \(view)")
                return view as? UIRefreshControl
            }
        }
        return nil
    }

    func addRefreshControlWith(sender: UIViewController, action: Selector, view: UIView)
    {
        guard findRefreshControl() == nil else { return }

        let refreshControl = UIRefreshControl()
        refreshControl.addTarget(sender, action: action, for: UIControlEvents.valueChanged)

        view.addSubview(refreshControl)
    }

    func showRefreshControlWith(attributedTitle: String? = nil)
    {
        findRefreshControl()?.attributedTitle = NSAttributedString(string: attributedTitle!)
        findRefreshControl()?.beginRefreshing()
    }

    func hideRefreshControl ()
    {
        findRefreshControl()?.endRefreshing()
    }
}

ViewController中的被调用方:(由观察事件触发)
private func showLoader()
    {
        tableView.showRefreshControlWith(attributedTitle: "Loading Profile".localized())
    }

    private func hideLoader()
    {
        tableView.hideRefreshControl()
    }

观察员:
_ = presenter.profilePictureUploadingPending.skip(first: 1).observeNext { _ in self.showLoader() }
_ = presenter.profilePictureUploaded.skip(first: 1).observeNext { _ in self.hideLoader() }

但是,当我进入UIImagePicker在我的应用程序中上载照片并返回viewController时,它应该再次触发旋转过程,直到照片上载成功。
我调试了我的代码,它正在启动方法,但微调器从视图层次结构子视图中消失,并且没有出现。。。
我不知道如何解决这个问题,但我真的很感谢你们的帮助!
谢谢,
凯文。

最佳答案

用以下方法解决:

private func showLoader()
    {
        DispatchQueue.global(qos: .default).async {
            DispatchQueue.main.async {
                self.tableView.contentOffset.x = 1
                self.tableView.contentOffset.y = -81
                self.tableView.showRefreshControlWith(attributedTitle: "LOADING_PROFILE".localized())
            }
        }
    }

10-08 07:45
查看更多