kharrison / CodeExamples

Code Examples
https://useyourloaf.com
BSD 3-Clause "New" or "Revised" License
2.13k stars 959 forks source link

UIRefreshControl not working #10

Closed CainLuo closed 5 years ago

CainLuo commented 5 years ago

Hello, There is a question about UIRefreshControl, I tried to rewrite your RefreshScroll Project in Xcode 12.2.1, but found that UIRefreshControl needs manual pull to work, and your Demo directly calls scrollView.refreshControl?.beginRefreshing() Can work, what are you doing?

class SignUpViewController: UIViewController {

    @IBOutlet private weak var scrollView: UIScrollView!
    @IBOutlet private weak var orangeLabel: UILabel!
    @IBOutlet private weak var orangeButton: UIButton!

    private var orangeAvailable = true {
        didSet {
            orangeLabel.isHidden = !orangeAvailable
            orangeButton.isHidden = !orangeAvailable
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        if #available(iOS 10.0, *) {
            let refreshControl = UIRefreshControl()
            let title = NSLocalizedString("PullToRefresh", comment: "Pull to refresh")
            refreshControl.attributedTitle = NSAttributedString(string: title)
            refreshControl.addTarget(self,
                                     action: #selector(refreshOptions(sender:)),
                                     for: .valueChanged)
            scrollView.refreshControl = refreshControl

            scrollView.refreshControl?.beginRefreshing()
        }
    }

    @objc private func refreshOptions(sender: UIRefreshControl) {
        orangeAvailable = !orangeAvailable
//        sender.endRefreshing()
    }
}

Even more strange is that I copied the Main.storyboard file of the RefreshScroll Project to my Demo Project and it works fine.

kharrison commented 5 years ago

where are you seeing the call tobeginRefreshing in my code? The line scrollView.refreshControl?.beginRefreshing() does not exist in my viewDidLoad method. The refresh control is triggered by the user pulling the scroll view down.

CainLuo commented 5 years ago

This is the demo code I wrote. Now I have found the problem and solved it. Thank you