Home collectionView scrollToItemAtIndexPath not scrolling down Initially when keyboard is visible

# collectionView scrollToItemAtIndexPath not scrolling down Initially when keyboard is visible

user47165
1#
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.

• 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
StackExchange.ready(function(){\$.get('/posts/31497338/ivc/c735');}); 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.
Processed in 0.488915 second(s) , Gzip On .