Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift pull to refresh

i have my refreshcontroller with a scrollview..

  self.refreshControl = UIRefreshControl()
        self.refreshControl.attributedTitle = NSAttributedString(string: "Frissítéshez húzzad! :)")
        self.refreshControl.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)
        self.scrollView.addSubview(refreshControl)

func refresh(sender:AnyObject)
    {

//my refresh code here..

            self.refreshControl.endRefreshing()
    }

I'm adding as a subview to a scrollView. It's working when the page content is overflowing the screen. but the issue arise when i haven't received much data and it's not overflowing, the pull and refresh function is not working. :(

I'm not using tableview.

Can anybody help me how to solve this problem?

like image 605
solarenqu Avatar asked Apr 22 '15 08:04

solarenqu


People also ask

How do I pull to refresh?

Pull-to-refresh is a touchscreen gesture that retrieves all the latest data and updates the currently available data in the app. You initiate it by swiping down from the top of the screen. This action will load new data from the server and display it in the interface.

How do you add a pull to refresh to a table view or collection view?

You can add a refresh control to a table or collection view by assigning an instance of the UIRefreshControl class to this property. If your application targets versions prior to iOS 10, you simply add the refresh control as a subview to the table view. The table view takes care of the rest.


2 Answers

self.scrollView.scrollEnabled = true
self.scrollView.alwaysBounceVertical = true

var alwaysBounceVertical: Bool // default NO. if YES and bounces is YES, even if content is smaller than bounds, allow drag vertically

like image 150
Hugo Avatar answered Oct 23 '22 21:10

Hugo


iOS 10 Update

alwaysBounceVertical is not entirely true, and refreshControl has been introduced:

  1. All iOS versions: bounces is always required if contentSize is smaller than frame
  2. Pre iOS 10 versions: alwaysBounceVertical is also required for small content
  3. iOS 10+: notify the UIScrollView of the existence of a UIRefreshControl using refreshControl

- iOS 10

UIRefreshControl is now supported by UIScrollView, using refreshControl just like UITableView in previous OS.
This means that the drag & pull down experience, is flawless, without drift.
Follow the tap + drag on the white arrow in the animation below: they stay in sync

iOS 10

↻ replay animation

- iOS 9 and earlier

You can add a UIRefreshControl manually to a UIScrollView, but that view has no awareness of such an element, and the pull to refresh tends to drift.
Notice how much harder it is to pull to refresh on the animation below: scrolling of the white arrow drifts, and requires a much greater distance to trigger the control

iOS 9

↻ replay animation


Bypass refreshControl OS discrepancies with this scroll view extension:

var _refreshControl : UIRefreshControl? {
    get {
        if #available(iOS 10.0, *) {
            return refreshControl
        } else {
            return subviews.first(where: { (view: UIView) -> Bool in
                view is UIRefreshControl
            }) as? UIRefreshControl
        }
    }

    set {
        if #available(iOS 10.0, *) {
            refreshControl = newValue
        } else {
            // Unique instance of UIRefreshControl added to subviews
            if let oldValue = _refreshControl {
                oldValue.removeFromSuperview()
            }
            if let newValue = newValue {
                insertSubview(newValue, at: 0)
            }
        }
    }
}

► Find this solution on GitHub.

like image 31
SwiftArchitect Avatar answered Oct 23 '22 20:10

SwiftArchitect