Set minimum contentsize for UICollectionView

风格不统一 提交于 2019-12-01 07:31:27
khangsile

You can do this by subclassing UICollectionViewFlowLayout and overriding the method

-(CGSize)collectionViewContentSize
{ //Get the collectionViewContentSize
     CGSize size = [super collectionViewContentSize];
     if (size < minimumSize) return minimumSize;
     else return size;
}

Edit: I just realized you said you didn't want to subclass the layout. Anyway, I subclassed UICollectionViewFlowLayout and only modified the collectionViewContentSize method. It kept the standard vertical layout for me.

Edit: https://stackoverflow.com/a/14465485/2017159. Here it says UICollectionViewFlowLayout only supports one direction (vertical or horizontal), so it should be fine?

you can try this quick solution, the search bar will be hidden if you have enough items to fill the screen. Of course you can change the UISearchBar below with any custom view.

collectionView.contentInset = UIEdgeInsetsMake(44.0, 0.0, 0.0, 0);
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, -44, collectionView.frame.size.width, 44)];
[collectionView addSubview:searchBar];
if([items count] != 0){
    [collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionTop animated:NO];
}

another solution which does exactly the same thing is to use supplementary views. I just had a go. Create a subclass of UICollectionReusableView, make sure you set the header reference size on the flow layout

[flowLayout setHeaderReferenceSize:CGSizeMake(0, 44.0)];  

register the supplementary view with collection view

[playersCollectionView registerClass:[MySupplementaryView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MyHeader"];

and implement the UICollectioViewDataSource method

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    MySupplementaryView *header = nil;

    if ([kind isEqual:UICollectionElementKindSectionHeader]){
        header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"MyHeader" forIndexPath:indexPath];
        header.headerLabel.text = @"bla bla";
    }
    return header;
}

And finally after each reload, reposition the collection view at the start of the first item to hide the searchBar/header view.

if([items count] != 0){
    [collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionTop animated:NO];
}

Tutorials for supplementary views techotopia.com, appcode.com, mobinius

This is a tweaked version of khangsile's answer. Capping only the dimentions that are actually smaller than the minimum size

- (CGSize)collectionViewContentSize
{
    CGSize size = [super collectionViewContentSize];

    size.width  = MAX(size.width, self.minimumContentSize.width);
    size.height = MAX(size.height, self.minimumContentSize.height);

    return size;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!