18

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

electronix384128
  • 6,625
  • 11
  • 45
  • 67
  • 1
    How are you setting the bottom constraint? This problem most likely has to do with the size of the collection view and little/nothing to do with the content inset. – nhgrif Jun 02 '14 at 11:33
  • 1
    Take a screenshot to get an idea about layout you have added – E-Riddie Jun 02 '14 at 11:34
  • 1
    I think your _collectionView height is the issue. you should set the height upto the view down the screen – Suryakant Sharma Jun 02 '14 at 11:35
  • ok, i added a screenshot of my IB setup... see above... – electronix384128 Jun 02 '14 at 11:39
  • 2
    Well, I want to have the translucent effect of the UIToolbar when the messages scroll underneath it. For that reason, the UICollectionView goes underneath the UIToolBar and I am setting the contentInset. – electronix384128 Jun 02 '14 at 12:13

6 Answers6

35

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)];
}
electronix384128
  • 6,625
  • 11
  • 45
  • 67
  • Spent hours on this and my previous solution was deprecated of late, thank you! _SO MUCH!_ – agrippa Aug 28 '17 at 09:42
  • hooray works perfectly :D -- im also fairly sure the exact same applies for UITableView – joe Jan 27 '18 at 00:46
13

If you are using flow layout try to set _collectionView.collectionViewLayout.sectionInset.

Arjuna
  • 697
  • 4
  • 17
4

swift version

collectionview.contentInset = UIEdgeInsetsMake(44,0,0,0)
collectionview.scrollIndicatorInsets = UIEdgeInsetsMake(44,0,0,0)
Yoni Katz
  • 107
  • 1
2

Possible Issue

You have set the collectionView below the toolbar and added constraints to bottom of superview for both views.

Solution

Set the constraints of the toolbar to bottom, leading and set the width and height to fixed size. For your collectionView set the constraints to top, to bottom with the toolbar, leading to the superview and (alternative) with fixed size of width

Update

CollectionView

Follow these steps to make it work:

Check your collection view, and don't put it below toolbar, and add these constraints by selecting your collectionView on Document Outline, click ctrl and drag it to your view, a popup will appear, hold shift and select these constraints.

CollectionView Constraints

Toolbar

Check the leading and bottom, by dragging with ctrl in view. And add fixed width and height for toolbar.

Toolbar Constraints

Dealing with scrolling before viewAppears

-(void)viewWillAppear:(BOOL)animated
{
    [collectionView reloadData];
    [self scrollToLastMessageAnimated:YES];
}
Community
  • 1
  • 1
E-Riddie
  • 14,660
  • 7
  • 52
  • 74
  • It does not help... It is not a UITabBar int his case, but a UIToolBar which is at the bottom... Any more ideas? – electronix384128 Jun 02 '14 at 11:33
  • Set the constraints of the toolbar to bottom, leading and set the width and height to fixed size. For your collectionView set the constraints to top, to bottom with the toolbar, leading to the superview and (alternative) with fixed size of width – E-Riddie Jun 02 '14 at 11:38
  • Your approach is giving me the same behavior :( Also then the UIScrollView would not be underneath the UIToolbar, lacking the translucent effect... – electronix384128 Jun 02 '14 at 12:12
  • Ok, that works better! Two problems though: 1) My scroll to bottom method does not work in viewWillAppear: anymore - only in viewDidAppear: making the initial scrolling to Bottom (scrollToLastMessageAnimated:) visible to the user, which is unwanted. 2) The translucent effect of the UICollectionView behind the UIToolBar and the keyboard is gone... – electronix384128 Jun 02 '14 at 14:46
  • which scrollView are you referring to? The collectionView's scroll? – E-Riddie Jun 02 '14 at 14:49
  • Yes, i am scrolling all the way down to the last message, that is displayed in the UICollectionView... See my scroll method in the initial question... – electronix384128 Jun 02 '14 at 14:51
  • http://stackoverflow.com/questions/12679182/uicollectionview-with-flowlayout-not-scrolling-when-setting-section-insets refer to this question – E-Riddie Jun 02 '14 at 14:53
  • No, your link is about a collectionView not scrolling. But mine is scrolling perfectly. The only problem right now, is that the initial scroll, when the view loads, is visible to the user, which is unwanted! – electronix384128 Jun 02 '14 at 15:04
  • I guess, I should probably mark your answer as correct, since I did not mention anything about the scrolling in my initial question :/ – electronix384128 Jun 02 '14 at 15:05
  • @BenMarten if I am understanding it correctly, `[collectionView setShowsHorizontalScrollIndicator:NO]; [collectionView setShowsVerticalScrollIndicator:NO];` – E-Riddie Jun 02 '14 at 15:12
  • sry, no... its about the initial scrolling... when there are already lets say 50 messages in the collection view. I need to scroll down. When the view first loads, without animation! – electronix384128 Jun 02 '14 at 15:15
  • @BenMarten I think you need to reload collectionView before on viewWillAppear, after call your scrolling method :). Answer updated – E-Riddie Jun 02 '14 at 15:21
  • @BenMarten why are you removing your votes and accepted answer? – E-Riddie Jun 04 '14 at 17:14
  • Because the correct answer is actually marked now! Sorry, but anyway thanks for your effort... – electronix384128 Jun 04 '14 at 17:38
  • I am OK with your accepted asnwer since it works, but removing your vote up was not necessary. Anyway I am glad you have found your solution! – E-Riddie Jun 04 '14 at 17:40
2

You can set the referenceSizeForFooterInSection function in your viewController class:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
    return CGSize(width: view.frame.width, height: 50)
}

Just set the height to your required value.

T. Christiansen
  • 1,036
  • 2
  • 19
  • 34
0

Programmatically, I solved this by using (Swift 5) :

collectionView.contentInsetAdjustmentBehavior = .never
Jerem Lachkar
  • 1,008
  • 11
  • 24