Yuuu
Yuuu

Reputation: 879

UIRefreshControl sometimes gives me a weird behavior

I added the UIRefreshControl to the collection view (where the view controller has navigation bar, tabbar and collection view), and mostly it works as it should be. (drag down the collection view, show the refresh control spinning, and then place back the collection view to the original position.) The issue I have happens not always, but I feel it happens once in four times. Here is my issue.

While I drag the collection view down, it shows the refresh controller at the top of the screen. (this is fine)

enter image description here

But when I quickly drag the collection view down till near the bottom of the screen, the navigation title (TEST in the picture) and collection view are sometimes placed back to the bottom of the refresh control (but this happens only for a moment), like the image below.

enter image description here

Then right after the collection view and navigation bar are placed right below the refresh controller, the collection view and navigation title are back to where I was dragging down again.

enter image description here

Hope the description above makes sense to you. But again, the issue, navigation title & collection view are placed right under the refresh control while dragging, happens only for a moment, and it happens not always.

So if anyone knows why this happens, please let me know!

Here is my code.

class LiveListViewController: UIViewController, UICollectionViewDelegate, AlertDisplayDelegate, EventAdditionDelegate {

    private let refreshControl = UIRefreshControl()
    private var collectionView: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()
        var layoutConfig = UICollectionLayoutListConfiguration(appearance: .insetGrouped)
        let listLayout = UICollectionViewCompositionalLayout.list(using: layoutConfig)
        collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: listLayout)
        view.addSubview(collectionView)
        
        setupNavbar()
        setupCollectionView()
        configureDelegate()
        addRefreshControl()
        displayLiveEvents()
        configureDataSource()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        lockAndRotateOrientationToPortrait()
    }
    
    // MARK: - Delegate
    
    func configureDelegate() {
        collectionView.delegate = self
    }
    
    // MARK: - Setup Collection View
    func setupCollectionView() {
        collectionView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            collectionView.topAnchor.constraint(equalTo: view.topAnchor),
            collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
        ])
    }
    
//    // MARK: - Refresh Control
    func addRefreshControl() {
        refreshControl.addTarget(self, action: #selector(displayListWithRefreshControl), for: .valueChanged)
        
        if #available(iOS 10.0, *) {
            collectionView.refreshControl = refreshControl
        } else {
            collectionView.addSubview(refreshControl)
        }
    }

    @objc func displayListWithRefreshControl() {
        dataSource.fetchLiveEvents { [weak self] in
            self?.populateCV()
            DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {
                self?.collectionView.refreshControl?.endRefreshing()
            })
        }
    }
    
    
}

Added the video

This is the video.

In the video, I refreshed twice. The first one is no problem at all. But the second, however, you can see the collection view looks live bouncing when I drag down. When I take a close look at the video, I can see the collection view is bounce back to the bottom of the refresh control only for a moment and then back to where it should be.

This is the slow-motion video of the issue.

※ I checked the video, and sometimes I was not able to see the issue in the video, so if it happens, please reload the video and check it again.

Upvotes: 1

Views: 272

Answers (0)

Related Questions