• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 设置 contentInset 后 UICollectionView 不滚动

[复制链接]
菜鸟教程小白 发表于 2022-12-13 02:26:55 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个水平滚动并跨越其父 View 的整个宽度的 Collection View 。我实现分页的廉价方法是将单元格宽度设置为 Collection View 宽度的 1/3,并设置与左右内容插图相同的宽度。

我在 IB 中禁用滚动并用左右滑动识别器替换。我的代码几乎可以在不设置 contentInset 的情况下工作,但设置 contentInset 似乎可以防止任何滚动发生

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    CGFloat itemWidth = self.collectionView.bounds.size.width/3.0;
    NSInteger count = [self collectionView:self.collectionView numberOfItemsInSection:0];
    self.collectionView.contentSize = (CGSize){ .width=itemWidth*count, .height=self.collectionView.bounds.size.height };
    // uncomment this line, and the scroll code in the swipes below fails to work
    //self.collectionView.contentInset = UIEdgeInsetsMake(0, itemWidth, 0, itemWidth);
    self.collectionView.contentOffset = (CGPoint){ .x=self.collectionView.contentSize.width/2.0, .y=0 };
}

- (CGSize)collectionViewUICollectionView *)collectionView layoutUICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPathNSIndexPath *)indexPath {
    CGFloat width = self.view.bounds.size.width/3.0;
    return (CGSize){ .width=width, .height=collectionView.bounds.size.height };
}

此代码处理滑动...

- (NSIndexPath *)centerIndexPath {
    CGRect visibleRect = (CGRect){.origin = self.collectionView.contentOffset, .size = self.collectionView.bounds.size};
    CGPoint visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect));
    return [self.collectionView indexPathForItemAtPoint:visiblePoint];
}

- (void)swipeLeftUISwipeGestureRecognizer *)gr {
    NSIndexPath *centerIndexPath = [self centerIndexPath];
    NSLog(@"at %@", centerIndexPath);

    if (centerIndexPath.row < [self collectionView:self.collectionView numberOfItemsInSection:0]-1) {
        [self.collectionView scrollToItemAtIndexPath:centerIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
    }
}

- (void)swipeRightUISwipeGestureRecognizer *)gr {
    NSIndexPath *centerIndexPath = [self centerIndexPath];
    NSLog(@"at %@", centerIndexPath);

    if (centerIndexPath.row > 0) {
        [self.collectionView scrollToItemAtIndexPath:centerIndexPath atScrollPosition:UICollectionViewScrollPositionRight animated:YES];
    }
}

除了我在上面的设置中设置 contentInsets 之外,所有这些都有效。然后,即使我到达调试器中的 scrollToItemAtIndexPath: 代码,也不会发生滚动。

拥有这些插图很重要,因为我希望用户了解中心项目是选定项目。

谁能解释为什么 contentInset 会破坏滚动以及如何解决?



Best Answer-推荐答案


看起来 UICollectionView 有自己的内置方式来处理插入:

https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/UsingtheFlowLayout/UsingtheFlowLayout.html#//apple_ref/doc/uid/TP40012334-CH3-SW1

使用部分插图调整内容的边距 部分插图是一种调整可用于布置单元格的空间的方法。您可以使用 insets 在节的页眉 View 之后和页脚 View 之前插入空格。您还可以使用插图在内容的两侧插入空间。图 3-5 演示了插入如何影响垂直滚动流布局中的某些内容。

图 3-5 部分插图更改了用于布置单元格的可用空间

enter image description here

关于ios - 设置 contentInset 后 UICollectionView 不滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45580623/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap