如何添加拉刷新控制器到scrollview?

时间:2019-02-17 06:08:34

标签: ios swift scrollview pull-to-refresh

您好,我想在应用程序中添加“拉动”以刷新控制器来滚动视图。 我编写了以下代码,但没有任何反应。

当我每次滚动视图时,都不会调用添加目标方法。

@IBOutlet var scrool: UIScrollView!
var refreshControl = UIRefreshControl()

override func viewDidLoad() {
        super.viewDidLoad()
        // Add refresh control for the Home page scroll view.
        self.refreshControl.tintColor = UIColor.appcolor
        self.refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
        self.refreshControl.addTarget(self, action: #selector(refresh(sender:)), for: UIControl.Event.valueChanged)
        self.scrool.isScrollEnabled = true
        self.scrool.alwaysBounceVertical = true
        scrool.addSubview(refreshControl)
    }
    @objc func refresh(sender:AnyObject) {
        // Code to refresh table view
        self.getHomePageApi()
        refreshControl.endRefreshing()

    }

1 个答案:

答案 0 :(得分:0)

嗨,当我在viewwillappear()中编写此代码时,除了viewdidload()函数可以正常工作,我已经解决了这个问题。

var refreshControl:RefreshControl!    
override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        refreshControl = RefreshControl()
        refreshControl.addTarget(self, action: #selector(refresh), for: .valueChanged)
        scrool.refreshControl = refreshControl
    }

    @objc func refresh()
    {
        // Code to refresh table view
        refreshControl.endRefreshing()
        self.getHomePageApi()

    }