Home > Software design >  UIRefreshControl - does the scroll view need to be "full" for this to work?
UIRefreshControl - does the scroll view need to be "full" for this to work?

Time:03-10

I have developers working on an IOS app that has a grid of people in a view (a dating style app). I need "pull to refresh" to work on this screen, even if the users in the grid do not fill the screen yet. ie: if there are only 2-4 users, the grid isn't full enough yet to scroll.

My iOS developer is telling me that iOS "pull to refresh" will not work if the grid is not full and scrollable. Is this true? Shouldn't pull to refresh work regardless if the screen is full? Or how can this easily be programmed?

Thank you.

CodePudding user response:

Here is about as basic of an example as you can get...

class ViewController: UIViewController {

    let scrollView = UIScrollView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(scrollView)
        
        let g = view.safeAreaLayoutGuide
        
        NSLayoutConstraint.activate([

            // let's constrain the scroll view with 20-points on all sides
            //  so we can easily distinguish it from the main view
            scrollView.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0),
            scrollView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
            scrollView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
            scrollView.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: -20.0),

        ])
        
        // give it a background color so we can see it
        scrollView.backgroundColor = .red
        
        // Add the refresh control
        scrollView.refreshControl = UIRefreshControl()
        scrollView.refreshControl?.addTarget(self, action: #selector(handleRefreshControl), for: .valueChanged)

    }

    @objc func handleRefreshControl() {
        // do whatever you do to get new content
        print("refresh the content")
        
        // let's simulate a 1-second task to get new content
        DispatchQueue.main.asyncAfter(deadline: .now()   1.0) {
            self.scrollView.refreshControl?.endRefreshing()
        }
    }

}

Note that we didn't even add any subviews to the scroll view, and the refresh control still works.

CodePudding user response:

Try setting alwaysBouncesVertical to true.

From the docs for UIScrollView (which is a parent of UITableView):

If this property is set to true and bounces is true, vertical dragging is allowed even if the content is smaller than the bounds of the scroll view. The default value is false.

https://developer.apple.com/documentation/uikit/uiscrollview/1619383-alwaysbouncevertical

  • Related