ios UICollectionView 不滚动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16069357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:14:07  来源:igfitidea点击:

UICollectionView does not scroll

iphoneiosuicollectionview

提问by Dario

I have a UICollectionViewset up with a UICollectionViewDataSourcethat currently provides six items. These are fewer than needed to fill the screen. The problem is that my collection view only scrolls when there are enough items to fill the screen (tested with 10, 20). When displaying fewer items it wont even do this bounce animation I am trying to get, it's just fixed.

我有一个UICollectionView设置与UICollectionViewDataSource目前提供6个项目。这些比填满屏幕所需的要少。问题是我的集合视图只在有足够的项目填满屏幕时滚动(用 10、20 测试)。当显示较少的项目时,它甚至不会执行我试图获得的反弹动画,它只是固定的。

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateCollectionViewData) name:UIDocumentStateChangedNotification object:nil];

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(160, 100);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;

    self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.bounces = YES;
    [self.view addSubview:self.collectionView];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [self.collectionViewData count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
     UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

    Expense *expense = [self.collectionViewData objectAtIndex:indexPath.row];

    UILabel *label = [[UILabel alloc]initWithFrame:cell.bounds];
    label.text = expense.value;
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont fontWithName:@"Miso-Bold" size:30];
    label.textAlignment = NSTextAlignmentCenter;
    [cell addSubview:label];

    cell.backgroundColor = [UIColor colorWithRed:1 - (indexPath.row / 30.0f) green:0 blue:1 alpha:1];

    return cell;
}

Thanks for your help!

谢谢你的帮助!

回答by jrturton

bounces, despite it's name, isn't the right property to set. You also need to set alwaysBounceVerticaland / or alwaysBounceHorizontal. From the documentation:

bounces,尽管它的名字,不是正确的属性设置。您还需要设置alwaysBounceVertical和/或alwaysBounceHorizontal. 从文档:

If this property is set to YES and bounces is YES, vertical dragging is allowed even if the content is smaller than the bounds of the scroll view. The default value is NO.

如果此属性设置为 YES 并且反弹为 YES,则即使内容小于滚动视图的边界,也允许垂直拖动。默认值为否。



Note the confusing name in IB .. https://stackoverflow.com/a/18391029/294884

请注意 IB 中令人困惑的名称 .. https://stackoverflow.com/a/18391029/294884

回答by Vladimir Shutyuk

With storyboards in attributes inspector for collection view "Bounces" and "Bounces Vertically" should be checked.

对于集合视图的属性检查器中的故事板,应该检查“Bounces”和“Bounces Vertically”。

回答by Nishant Tyagi

Setting the height of the UICollectionViewto size of UIViewwill make your scrolling problem disabled. If the UICollectionViewis 568 pixels tall then it will only ever need to scroll if it has more than 568 pixels worth of content in it. You should set it to the height of the view it is contained in (same as the width).

将高度设置UICollectionView为大小UIView将使您的滚动问题被禁用。如果UICollectionView568 像素高,那么它只需要在其中包含超过 568 像素的内容时才需要滚动。您应该将其设置为包含它的视图的高度(与宽度相同)。

Hope it helps you.

希望对你有帮助。