Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIRefreshController goes over the UICollectionView

This is a bug that happens frequently, but not always. I have a refresh control added to my UICollectionView. When I refresh, it works perfectly. However, if I half refresh it several times or even do a full refresh, go to another tab in the app, and then return back, when I refresh, the UIRefreshControl appears over part of the UICollectionView. Additionally, instead of starting from zero refresh spinner bars, it starts with all loaded (I've noticied this in other apps such as Mail though, so that much is an OS bug, but in the OS apps, the spinner does not go over the content). Here's an image of the problem: https://www.dropbox.com/s/4qk6qjrdlapsvz0/IMG_0074.JPG Here's how I set up the RefreshController in ViewDidLoad.

refreshControl = [[UIRefreshControl alloc] init];

[refreshControl addTarget:self action:@selector(refresh2)

forControlEvents:UIControlEventValueChanged];

[self.collectionView addSubview:refreshControl];

self.collectionView.alwaysBounceVertical = YES;

Anyone know how to make the spinner go behind the CollectionViewController? Thanks

like image 409
user1941966 Avatar asked Feb 25 '14 02:02

user1941966


People also ask

What is a UICollectionView?

An object that manages an ordered collection of data items and presents them using customizable layouts.

How does swift implement UICollectionView?

Select the Main storyboard from the file browser. Add a CollectionView by pressing command shift L to open the storyboard widget window. Drag the collectionView onto the main view controller. Add constraints to the UICollectionView widget to ensure that the widget fills the screen on all devices.

How do you add a pull to refresh in collection view in Swift?

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.

What is refresh control swift?

Overview. A UIRefreshControl object is a standard control that you attach to any UIScrollView object, including table views and collection views. Add this control to scrollable views to give your users a standard way to refresh their contents.


1 Answers

Problem: You start to scroll down and partially reveal the UIRefreshControl, but you don't scroll all the way down to make it start spinning. You navigate to another view (or "minimize" app, send to background) and come back. You scroll a little and the UIRefreshControl shows on top of your collectionview fully loaded, unwelcomed and not spinning.

Solution: You need to endRefreshing on viewWillDissappear and on app being sent to background.

var refreshControl = UIRefreshControl()

override func viewDidLoad() {
    super.viewDidLoad()
    refreshControl.addTarget(self, action: #selector(ViewController.methodNameForRefreshing), forControlEvents: .ValueChanged)
    collectionView.addSubview(refreshControl)
    //or
    tableView.addSubview(refreshControl)

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.willResignActiveNotif), name: UIApplicationWillResignActiveNotification, object: nil)
}

override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)
    refreshControl.endRefreshing()
}

func willResignActiveNotif(notification: NSNotification) {
    refreshControl.endRefreshing()
}
like image 146
sweepez Avatar answered Sep 28 '22 04:09

sweepez