Home collectionView scrollToItemAtIndexPath not scrolling down Initially when keyboard is visible
Reply: 0

collectionView scrollToItemAtIndexPath not scrolling down Initially when keyboard is visible

user47165 Published in September 19, 2018, 7:20 pm

Was hoping somebody can help me with this very frustrating behaviour.

My UICollectionView scrollToItemAtIndexPath works perfectly in all scenarios EXCEPT when the keyboard is visible and the content size is smaller than a value of about 780 px in height. After the content size grows beyond this the scroll works as it should and at all times when the keyboard is hidden.

My entire collectionView along with other views are embedded in a scrollView.

The keyboardWillShow method contains the following:

        UIView.animateWithDuration(animationDuration, animations: { () -> Void in
            self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, intersectionOfKeyboardRectAndWindowRect.size.height, 0)
            self.collectionView.contentInset = UIEdgeInsetsMake(intersectionOfKeyboardRectAndWindowRect.size.height, 0, 0, 0)
            self.scrollView.scrollRectToVisible(self.messageView.frame, animated: false)

If I remove the second line which defines the collectionView offset then the behaviour is normal on the bottom but the top of the collectionView is hidden under the top bar.

I have tried to present Modally and the problem still exists. My method to scroll the collectionView is:

 private func updateCollectionView() {
     if self.messages.count > 0 {

         let indexPath = NSIndexPath(forItem: self.messages.count - 1, inSection: 0)
         self.collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Bottom, animated: false)

It almost seems as if the height of the collectionView is somehow set to a large number which ignores AutoLayout constraints and the scroll only becomes active when the content size exceeds that value.

share|improve this question
  • just wondering if I need to define either the collectionView content size or the scrollView content size somewhere and if so to what value? – alionthego Jul 19 '15 at 2:44
  • what's collectionView and scrollView? How is your view hierarchy? – ozgur Jul 19 '15 at 4:55
  • top level is a scrollView in which there are two subviews. a collectionView and a UIView containing a textfield. I chose to add the collectionView and textField View inside a scrollView so that I could have a chatMessage type interface. That way both the collectionView and textField View are moved up by the show keyboard method when the keyboard is displayed. – alionthego Jul 19 '15 at 5:08
  • if anybody can help out I would appreciate it. Have been working on this so long and really don't know what is wrong. – alionthego Jul 20 '15 at 0:26
  • hi @ozgur just wondering if you had a chance to look at it again? thanks. – alionthego Jul 20 '15 at 1:32

active oldest votes

Your Answer

StackExchange.ifUsing("editor", function () { StackExchange.using("externalEditor", function () { StackExchange.using("snippets", function () { StackExchange.snippets.init(); }); }); }, "code-snippets"); StackExchange.ready(function() { var channelOptions = { tags: "".split(" "), id: "1" }; initTagRenderer("".split(" "), "".split(" "), channelOptions); StackExchange.using("externalEditor", function() { // Have to fire editor after snippets, if snippets enabled if (StackExchange.settings.snippets.snippetsEnabled) { StackExchange.using("snippets", function() { createEditor(); }); } else { createEditor(); } }); function createEditor() { StackExchange.prepareEditor({ heartbeatType: 'answer', convertImagesToLinks: true, noModals: false, showLowRepImageUploadWarning: true, reputationToPostImages: 10, bindNavPrevention: true, postfix: "", onDemand: true, discardSelector: ".discard-answer" ,immediatelyShowMarkdownHelp:true }); } });
StackExchange.ready( function () { StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f31497338%2fcollectionview-scrolltoitematindexpath-not-scrolling-down-initially-when-keyboar%23new-answer', 'question_page'); } );

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged ios swift uicollectionview scrollview or ask your own question.

StackExchange.ready(function () { StackExchange.responsiveness.addSwitcher(); }) (function(i, s, o, g, r, a, m) { i['GoogleAnalyticsObject'] = r; i[r] = i[r] || function() { (i[r].q = i[r].q || []).push(arguments) }, i[r].l = 1 * new Date(); a = s.createElement(o), m = s.getElementsByTagName(o)[0]; a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga'); StackExchange.ready(function () { StackExchange.ga.init({ sendTitles: true, tracker: window.ga, trackingCodes: [ 'UA-108242619-1' ] }); StackExchange.ga.setDimension('dimension2', '|ios|swift|uicollectionview|scrollview|'); StackExchange.ga.setDimension('dimension3', 'Questions/Show'); StackExchange.ga.trackPageView(); }); /**/ var _qevents = _qevents || [], _comscore = _comscore || []; (function() { var ssl = 'https:' == document.location.protocol, s = document.getElementsByTagName('script')[0], qc = document.createElement('script'); qc.async = true; qc.src = (ssl ? 'https://secure' : 'http://edge') + '.quantserve.com/quant.js'; s.parentNode.insertBefore(qc, s); _qevents.push({ qacct: "p-c1rF4kxgLUzNc" }); /**/ var sc = document.createElement('script'); sc.async = true; sc.src = (ssl ? 'https://sb' : 'http://b') + '.scorecardresearch.com/beacon.js'; s.parentNode.insertBefore(sc, s); _comscore.push({ c1: "2", c2: "17440561" }); })();
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.488915 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO