【问题标题】:UICollectionView does not scrollUICollectionView 不滚动
【发布时间】:2023-04-02 08:44:01
【问题描述】:

我有一个UICollectionView 和一个UICollectionViewDataSource,目前提供六个项目。 这些比填满屏幕所需的要少。问题是我的收藏视图只有在有足够的项目填满屏幕时才会滚动(用 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;
}

感谢您的帮助!

【问题讨论】:

    标签: iphone ios uicollectionview


    【解决方案1】:

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

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


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

    【讨论】:

    • 我已添加,内容看起来可滚动,但仅会随着可见内容反弹。边缘的内容不会滚动到视图中。还有其他缺少的设置吗?
    【解决方案2】:

    在集合视图的属性检查器中使用故事板时,应检查“反弹”和“垂直反弹”。

    【讨论】:

      【解决方案3】:

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

      希望对你有帮助。

      【讨论】:

      • 在 IB 中添加约束以保持 UICollectionView 边缘与其父视图的边缘相同解决了我的问题
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-04
      • 2018-01-30
      • 2020-06-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多