I've been building on iOS 7 for a while now but I've yet to get this solved, I have a number of views with autolayout enabled that were created in Storyboard and are displayed with a standard UINavigationController
. The majority are fine, but the ones based on UICollectionView
always place themselves under the navigation bar, unless I set the translucency to NO
. I've tried the edgesExtended
trick but that doesn't seem to solve it, I don't necessarily mind having the translucency off but I'd like to solve it cleaner.
5 Answers
FYI if your UICollectionView is the root view in your view controller's hierarchy and your view controller has automaticallyAdjustsScrollViewInsets
set to YES (this is the default), then the contentInset should update automatically.
However, the scrollview's contentInset is only automatically updated if your scrollview (or tableview/collectionview/webview btw) is the first view in their view controller's hierarchy.
I often add a UIImageView first in my hierarchy in order to have a background image. If you do this, you have to manually set the edge insets of the scrollview in viewDidLayoutSubviews:
- (void) viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
CGFloat top = self.topLayoutGuide.length;
CGFloat bottom = self.bottomLayoutGuide.length;
UIEdgeInsets newInsets = UIEdgeInsetsMake(top, 0, bottom, 0);
self.collectionView.contentInset = newInsets;
}
-
8your comment about having it as the first view in the hierarchy is crucial - that made a huge difference in my case– kfmfe04Dec 30, 2014 at 15:19
-
Knowing that it isn't handled automatically because of the view controllers place in the hierarchy is a nice bit of additional knowledge... good to know its not something I've done wrong! Thanks. Jan 26, 2015 at 16:02
-
1You should be calling [super viewDidLayoutSubviews] somewhere in this function Apr 13, 2016 at 14:21
I had this problem before, just set the edge insents of the collection view with a top margin:
[self.myCollectionVC.collectionView setContentInset:UIEdgeInsetsMake(topMargin, 0, 0, 0)];
Where topMargin is the size of the nav bar, or whatever point you want the collection to start scrolling.
In this way, your collection view will start scrolling just below the navigation bar, and at the same time it will fill the whole screen and you will see it if your nav bar is translucent.
-
Perfect, thank you. I had a feeling it would be something along those lines but couldn't find any information on it, especially as I'd tried resizing the
UICollectionView
manually as well. Sep 19, 2013 at 13:45 -
5topMargin should also take into account the statusbar height (20 pts) Oct 21, 2013 at 16:54
I had this problem after ios 11, just set the contentInsetAdjustmentBehavior of UICollectionView to never:
self.collectionView.contentInsetAdjustmentBehavior = .never
I am using swift and xcode 7.3.1. I solved it by going to story board and selecting my Navigation Controller and then unchecking "Extend Edges" "Under Top Bards".
-(void) viewDidLoad{
[super viewDidLoad];
self.automaticallyAdjustsScrollViewInsets = NO; //added important
}
- (void) viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
CGFloat top = self.topLayoutGuide.length;
CGFloat bottom = self.bottomLayoutGuide.length;
UIEdgeInsets newInsets = UIEdgeInsetsMake(top, 0, bottom, 0);
self.collectionView.contentInset = newInsets;
}