A UIRefreshControl added to a UIScrollView never displays

I decided to use UIScrollView instead of UITableView in my application for many reasons. When I used UITableView, I was able to add a UIRefreshControl without any problems. However, when I use the same code in my UIScrollView, nothing happens. I tried several third-party retraining libraries, and none of them work. Any help is appreciated.

A 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 makes my API call and creates UIViews ("cards") that are added to the UIScrollView. Then I change the contentSize of the UIScrollView based on the total height of all the maps. Maps are added perfectly, and the scroll view acts as expected, except with respect to the UIRefreshControl.

Thanks in advance for your help!

+7
ios swift uiscrollview uirefreshcontrol
source share
1 answer

EDIT:
After some cheating, I found another solution that would allow the scrollbar to scroll (and trigger the update control), even if the contentSize is less than the height of the scrollView. Just set the alwaysBounceVertical property to true:

 self.scroll.alwaysBounceVertical = true 

OLD RESPONSE:
The size of your content must be larger than the scroll height 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) 

This will allow your RefreshControl to enter.

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

+12
source share

All Articles