Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIRefreshControl added to UIScrollView is never shown

I have decided to use UIScrollView instead of UITableView in my app for many reasons. When I was using UITableView, I was able to add a UIRefreshControl with no issue whatsoever. When I use the same code on my UIScrollView however, nothing happens. I have tried several third-party refresher libraries, and none seem to work. Any help is appreciated.

Source:

var scroll: UIScrollView!
var refreshControl: UIRefreshControl!

override func viewDidLoad()
{
    super.viewDidLoad()

    self.scroll = UIScrollView(frame: self.view.frame)
    self.scroll.contentSize = CGSize(width: self.view.bounds.width, height: 10)

    self.refreshControl = UIRefreshControl()
    self.refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
    //self.refreshControl.addTarget(self, action: "loadPostsIntoCards", forControlEvents: UIControlEvents.ValueChanged)
    self.scroll.addSubview(self.refreshControl)

    self.view.addSubview(self.scroll)

    //self.loadPostsIntoCards()
}

The loadPostsIntoCards method does my API call, and creates UIViews ("cards") that are added to the UIScrollView. I then change the contentSize of the UIScrollView based upon the total height of all the cards. The cards are added perfectly, and the scroll view acts as expected, except with regards to the UIRefreshControl.

Thanks in advance for any help!

like image 439
Connor Hicks Avatar asked May 26 '15 17:05

Connor Hicks


1 Answers

EDIT:
After some fooling around I found another solution that will allow the scrollview to scroll (and trigger the refresh control) even when the contentSize is smaller than the scrollView height. Just set the alwaysBounceVertical property to true:

self.scroll.alwaysBounceVertical = true

OLD ANSWER:
Your content size should be larger than your scrollview's height in order to make this work. Change this line:

self.scroll.contentSize = CGSize(width: self.view.bounds.width, height: 10)

To:

self.scroll.contentSize = CGSize(width: self.view.bounds.width, height: self.view.bounds.height+1)

That would allow your RefreshControl to kick in.

Or, in your case you should probably calculate the height of your cards and then set that as the height for your contentSize.

like image 140
donnywals Avatar answered Nov 09 '22 23:11

donnywals