在没有单元格的情况下启用UICollectionView的跳出/滚动

Win*_*ero 12 ios

由于某些目的(比如拉到刷新),我需要一个UICollectionView可以在没有单元格时弹跳或滚动 - 表示numberOfItemsInSection:返回0

我的代码是这样的:

...
        UICollectionViewFlowLayout *flowLayout =[[UICollectionViewFlowLayout alloc] init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        flowLayout.minimumInteritemSpacing = SPLIT_SPACE;
        flowLayout.minimumLineSpacing = SPLIT_SPACE;
        targetCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - TABBAR_HEIGHT)
                                                    collectionViewLayout:flowLayout];
        [targetCollection registerNib:[UINib nibWithNibName:@"DashboardCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"DashboardCell"];

        targetCollection.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        targetCollection.backgroundColor = [UIColor colorWithHex:@"#EAEAEA"];
        targetCollection.contentInset = UIEdgeInsetsMake(0, 0, 20, 0);
        targetCollection.alwaysBounceVertical = YES; 
...
    #pragma mark - UICollectionViewDataSource
    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
    {
        return 0;
    }
Run Code Online (Sandbox Code Playgroud)

但是,在测试时,这个空的UICollectionView不能弹跳也不能滚动.我怀疑它与空单元格有关,但我确实需要在没有单元格时启用弹跳或滚动.这类似于我的另一个问题:SVPullToRefresh无法提取空的UICollectionView

Wel*_*ang 50

像这样做:

self.collectionView.alwaysBounceVertical = YES;
Run Code Online (Sandbox Code Playgroud)

@VNJ在这里回答


Yuc*_*ong 14

在故事板中更改此设置:

在此输入图像描述


Sue*_*uen -3

 - (void)_edgeInsetsToFit {
    UIEdgeInsets edgeInsets = self.collectionView.contentInset;
    CGSize contentSize = self.collectionView.contentSize;
    CGSize size = self.collectionView.bounds.size;
    CGFloat heightOffset = (contentSize.height + edgeInsets.top) - size.height;
    if (heightOffset < 0) {
        edgeInsets.bottom = size.height - (contentSize.height + edgeInsets.top) + 1;
        self.collectionView.contentInset = edgeInsets;
    } else {
        edgeInsets.bottom = 0;
        self.collectionView.contentInset = edgeInsets;
    }
}
Run Code Online (Sandbox Code Playgroud)

我在layoutSubviews之后调用了该方法

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    _STAssetViewController *weakSelf = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 
        [weakSelf _edgeInsetsToFit]; 
    });
}
Run Code Online (Sandbox Code Playgroud)

你可以尝试这样