Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Full screen horizontally scrolling UICollectionView cells shift downwards when used in first displayed UIViewController

I'm creating a series of help pages that display when the user first starts the app. To do this, I have a programatically set up UIViewController that initializes a (programatically set up) UICollectionView the size of the view controller bounds. Each cell contains a fullscreen sized image.

When this view controller is pushed when there's already an existing view controller, it shows up fine. But, when this view controller is used as the initial root view controller for the app navigation controller, the collection view has the correct size and alignment, but the cells are shifted down about 10 pixels from the top of the screen, so that the collection view background shows through.

image of bug (note that red is the collection view background color)

If I set up the collection view in viewDidLoad, viewDidLayoutSubviews, or viewWillAppear, I get the same problem. I don't encounter this issue if I set up the collection view in viewDidAppear, but this doesn't work because the user will see a black screen before the collection view loads.

Here is the code that is displaying the view controller, in application:didFinishLaunchingWithOptions:launchOptions:

UIViewController* viewControllerToPush = [[OnboardingViewController alloc] initWithNibName:nil bundle:nil];
_nav = [[UINavigationController alloc] initWithRootViewController:viewControllerToPush];
[_nav setNavigationBarHidden:YES];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setRootViewController:_nav];
[self.window makeKeyAndVisible];

And here is the code that is setting up the collection view and layout:

UICollectionViewFlowLayout* layout = [[UICollectionViewFlowLayout alloc] init];
layout.minimumInteritemSpacing = 0;
layout.minimumLineSpacing = 0;
layout.itemSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height);
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

CGRect collectionViewFrame = self.view.bounds;
_collectionView = [[UICollectionView alloc] initWithFrame:collectionViewFrame collectionViewLayout:layout];
_collectionView.delegate = self;
_collectionView.dataSource = self;
_collectionView.pagingEnabled = YES;
_collectionView.showsHorizontalScrollIndicator = NO;

[_collectionView registerClass:[OnboardingCollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
[self.view addSubview:_collectionView];
like image 695
DivideByZer0 Avatar asked Apr 28 '15 21:04

DivideByZer0


2 Answers

I fixed this issue by setting the view controller's automaticallyAdjustsScrollViewInsets to NO in the init function. Thanks to https://stackoverflow.com/a/25352483/1370967 for the inspiration on this.

like image 110
DivideByZer0 Avatar answered Sep 23 '22 06:09

DivideByZer0


For Swift 5.5 use that -> collectionView.contentInsetAdjustmentBehavior = .never

like image 39
Leon Jakonda Avatar answered Sep 22 '22 06:09

Leon Jakonda