升级到了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之前就有这个问题,所以并不是新版才出现的,至于为什么之前正常,现在不正常。。。