2

desc:

i have a view controller that holds a collection view with 7 cells inside of it. when i display this controller, all works fine and it displays perfectly (controller A). on another controller (controller B) i have a container view that embeds controller A, with the collection view.

the problem:

when running the app and going into controller B (that embeds controller A), all controller A's sub views look good except of the collection view. in this case, instead of seeing my 7 cells i see only 6 and after tapping on one of the cells, the last cell appears in the cells row and now it looks good (?!). its like the collection view is being redrawn and i cant find the reason or how to fix it.

any help is appreciated.

Edit

that is my flow layout, which i call from viewDidLoad:

-(void) setFlowLayout {
    BOOL isIphone = (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPad);
    float baseWidth = [[UIScreen mainScreen] bounds].size.width;
    int width;

    width = floor(baseWidth / 7.0);
    float space = baseWidth - (width * 7.0f);
    space /= 7.0;

    UICollectionViewFlowLayout * flow = [[UICollectionViewFlowLayout alloc] init];

    if(isIphone) {
        flow.itemSize = CGSizeMake(width, kCellHeight);
    }
    else{
        flow.itemSize = CGSizeMake(width, kCellSizeIpad);
    }

    flow.minimumInteritemSpacing = space;
    flow.headerReferenceSize = CGSizeMake(_collectionDays.frame.size.width, kSpacing);
    flow.footerReferenceSize = CGSizeZero;
    flow.scrollDirection = UICollectionViewScrollDirectionVertical;

    [_collectionDays setCollectionViewLayout:flow];
}

i also use this to place the cells in the middle of the collection view:

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    CGFloat cellSpacing = ((UICollectionViewFlowLayout *) collectionViewLayout).minimumInteritemSpacing;
    CGFloat cellWidth = ((UICollectionViewFlowLayout *) collectionViewLayout).itemSize.width;
    NSInteger cellCount = [collectionView numberOfItemsInSection:section];
    CGFloat inset = (collectionView.bounds.size.width - (cellCount * (cellWidth + cellSpacing))) * 0.5;
    inset = MAX(inset, 0.0);

    return UIEdgeInsetsMake(0.0, inset, 0.0, inset);
}

this is the BAD part, not all 7 cells there

thats how it should look like

Max
  • 799
  • 1
  • 5
  • 17

1 Answers1

1

so no answer so far, but i found i way to fix it. i kept on trying to solve it, and i think it has something to do with the container resize while its being drawn.

i fixed it using a call to collection view's layoutIfNeeded from its parent view controller, in viewDidLayoutSubviews.

so if someone comes across it, its one way of fixing it.

would love to hear a better way\explanation.

-(void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    [_collectionDays layoutIfNeeded];
    [_collectionDays reloadData];
}
Max
  • 799
  • 1
  • 5
  • 17