UICollectionView修改滚动方向后cellForItemAtIndexPath不执行

升级到了Xcode9之后 ,出现了不少莫名奇妙的问题,之前可以的功能都突然不行了

项目中有用到UICollectionView,collectionView数据无法加载出来,如下2个方法均不执行

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

经过查证,发现是这个collectionView是水平滑动。重设了滑动的方向,竟然导致上面2个方法不执行

        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

就是这个一句不起眼的代码导致的!!!

解决方法:在对应的viewcontroller中加入
    self.automaticallyAdjustsScrollViewInsets = NO;

然后就正常了·····好吧。你赢了

automaticallyAdjustsScrollViewInsets官方的解释是
A Boolean value that indicates whether the view controller should automatically adjust its scroll view insets.
The default value of this property is YES, which lets container view controllers know that they should adjust the scroll view insets of this view controller’s view to account for screen areas consumed by a status bar, search bar, navigation bar, toolbar, or tab bar. Set this property to NO if your view controller implementation manages its own scroll view inset adjustments.

大致意思是:如果设置YES的话,根据所在屏幕区域的status bar, search bar, navigation bar, toolbar, or tab bar.来调整scroll view的insets。
设置为NO的话,自己修改布局,不要ViewController来控制。

查资料发现Xcode9之前就有这个问题,所以并不是新版才出现的,至于为什么之前正常,现在不正常。。。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,255评论 4 61
  • 因为要结局swift3.0中引用snapKit的问题,看到一篇介绍Xcode8,swift3变化的文章,觉得很详细...
    uniapp阅读 4,537评论 0 12
  • “这个社会风太大,伸手怕犯错,缩手怕错过”
    李小小R阅读 296评论 0 0
  • 1.独处 独处可以理解为独居;亦可以理解一个人单独生活,独处是一种处世的态度,是一种身心的自我调整。更是体现独立人...
    灵魂兽者阅读 231评论 0 0
  • 两个宝都很乖阅读 159评论 0 0