UICollectionView with ContentInset doesn't scroll all the way down

I set the content in a UICollectionView:

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

Then I scroll programmatically down to the bottom of the UICollectionView using 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 scrolls down, but ignores the value of the contentInset, which means that the last cells are below the specified content insert:

enter image description here The left image shows how it appeared after viewing. On the correct image, I manually scrolled on to the last message.

I use AutoLayout, any ideas why this is happening?

EDIT:

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

+7
ios objective-c autolayout scroll uicollectionview
source share
5 answers

Today, by chance, I found a solution!

Select a view controller and uncheck the "Customize scrolls" checkbox.

enter image description here

If this option is not set, iOS does not automatically correct your inserts in the view (and, possibly, its subspecies), which caused problems for me ... Uncheck the box and configure your scroll inserts as it is 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)]; } 
+24
source share

If you are using a flow layout, try setting _collectionView.collectionViewLayout.sectionInset.

+7
source share

Possible problem

You set collectionView under the toolbar and added restrictions to the bottom of the supervisor for both views.

Decision

Set the restrictions on the toolbars down, leading and set the width and height to a fixed size. For your View collection, set restrictions on the top, bottom using the toolbar, which will lead to supervision and (alternative) with a fixed width size

Update

Collectionview

To do this, follow these steps:

Check the collection view and do not place it under the toolbar and add these restrictions by selecting the View collection in the document structure, press ctrl and drag it into your view, a pop-up window will appear, hold shift and select these restrictions.

CollectionView Constraints

Toolbar

Check the top and bottom sides by dragging them with ctrl. And add a fixed width and height for the toolbar.

Toolbar constraints

Work with scrolling before view

 -(void)viewWillAppear:(BOOL)animated { [collectionView reloadData]; [self scrollToLastMessageAnimated:YES]; } 
+2
source share

You can set the referenceSizeForFooterInSection function to 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 the desired value.

+1
source share

quick version

 collectionview.contentInset = UIEdgeInsetsMake(44,0,0,0) collectionview.scrollIndicatorInsets = UIEdgeInsetsMake(44,0,0,0) 
+1
source share

All Articles