Fullscreen horizontal scroll. UICollectionView cells shift down when used in the first displayed UIViewController

I am creating a series of help pages that are displayed when the user first starts the application. For this, I have a programmatically configured UIViewController that initializes the (programmatically configured) UICollectionView size of the bounds of the view limiter. Each cell contains a full-screen image.

When this view controller is pressed, when an existing view controller already exists, it is displayed in order. But, when this view controller is used as the initial root view controller for the application navigation controller, the collection view has the correct size and alignment, but the cells are shifted about 10 pixels from the top of the screen, so the collection shows the background view.

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

If I set up the collection view in viewDidLoad, viewDidLayoutSubviews or viewWillAppear, I get the same problem. I do not encounter this problem if I configure the collection view in viewDidAppear, but this does not work because the user will see a black screen before loading the collection.

Here is the code that displays the view controller in the 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 sets 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];
+4
source share
1 answer

I fixed this problem by setting the view controller automaticallyAdjustsScrollViewInsets to NO in the init function. Thanks fooobar.com/questions/309540 / ... for the inspiration in this.

+7
source

All Articles