Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView with ContentInset is not Scrolling all the Way Down

I am setting the content inset of a UICollectionView:

[_collectionView setContentInset:UIEdgeInsetsMake(0.f, 0.f, 100.f, 0.f)];

Then I am scrolling programmatically all the way to the bottom of the UICollectionView with this method:

- (void)scrollToLastMessageAnimated:(BOOL)animated;
{
    if (_messages.count == 0) { return; }

    NSUInteger indexOfLastSection = _messagesBySections.count - 1;
    NSInteger indexOfMessageInLastSection = [_messagesBySections[indexOfLastSection] count] - 1;
    NSIndexPath *path = [NSIndexPath indexPathForItem:indexOfMessageInLastSection
                                            inSection:indexOfLastSection];

    [_collectionView scrollToItemAtIndexPath:path
                           atScrollPosition:UICollectionViewScrollPositionCenteredVertically
                                   animated:animated];
}

It is scrolling down, but it is ignoring the contentInset, meaning that the last cells are below the specified content inset:

enter image description here The left image, shows how it appear now after the view did appear. In the right image, I manually scrolled further down to the last message.

I am using AutoLayout, any ideas why this happens?

EDIT:

Here is a screenshot of the IB setup: enter image description here

like image 460
electronix384128 Avatar asked Jun 02 '14 11:06

electronix384128


1 Answers

Today, by chance I discovered the solution!

Select your view controller and uncheck the option "Adjust Scroll View Insets".

enter image description here

With this option unchecked, iOS does not automatically adjust your insets of the view (and probably its subviews), which caused the problems for me ... Uncheck it and configure your scroll insets like this programmatically:

- (void)configureInsetsOfCollectionView
{
    [_collectionView setContentInset: UIEdgeInsetsMake(self.navigationController.navigationBar.bounds.size.height + [UIApplication sharedApplication].statusBarFrame.size.height + DEFAULT_SPACING, 0.f, _keyboardHeight + _toolbar.bounds.size.height + DEFAULT_SPACING, 0.f)];
    [_collectionView setScrollIndicatorInsets:UIEdgeInsetsMake(self.navigationController.navigationBar.bounds.size.height + [UIApplication sharedApplication].statusBarFrame.size.height, 0.f, _keyboardHeight + _toolbar.bounds.size.height, 0.f)];
}
like image 184
electronix384128 Avatar answered Oct 14 '22 00:10

electronix384128