我有一个UICollectionViewController使用UICollectionViewFlowLayout,我的itemSizeUICollectionView的大小.基本上,这是一个单元格的行布局,其中每个单元格都是全屏的,并水平滚动.

在我的UICollectionViewFlowLayout子类中,我覆盖了prepareLayout,如下所示:

- (void)prepareLayout {
    self.itemSize = self.collectionView.frame.size;
    self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView.pagingEnabled = YES;
    self.minimumLineSpacing = 0.0;
    self.minimumInteritemSpacing = 0.0;
    self.sectionInset = UIEdgeInsetsZero;
    self.footerReferenceSize = CGSizeZero;
    self.headerReferenceSize = CGSizeZero;
}

UICollectionViewController是非常基本的,在一个部分返回10个项目.为了了解更多细节,我加入了sample project on GitHub.

一切似乎都设置正确.它在模拟器和设备上看起来是正确的,但当显示采集视图时,控制台上会记录一个错误:

the behavior of the UICollectionViewFlowLayout is not defined because:
the item height must be less that the height of the UICollectionView minus the section insets top and bottom values.

Note also that the collection view controller in my example is in a navigation controller and while that doesn't look particularly necessary in the example, in my real-world case I need the collection view in a navigation controller.

推荐答案

UIViewControllerautomaticallyAdjustsScrollViewInsets上有一个属性默认为YES.这意味着,当UIViewController在其视图层次 struct 中有UIScrollView时(对于UICollectionViewController是如此),该滚动视图的contentInset属性将自动调整,以考虑状态栏、导航栏和工具栏或选项卡栏所使用的屏幕区域.

该物业的文件说明:

automaticallyAdjustsScrollViewInsets

指定视图控制器是否应自动调整其滚动视图插入.

@property(nonatomic, assign) BOOL automaticallyAdjustsScrollViewInsets

Discussion

默认值为"是",这允许视图控制器根据状态栏、导航栏和工具栏或选项卡栏使用的屏幕区域调整其滚动视图插入.如果您想自己管理滚动视图插入调整,如

解决方案是在UICollectionViewController子类中的某个地方将automaticallyAdjustsScrollViewInsets设置为NO,例如在viewDidLoad中:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
}

我用了example project on GitHub分来说明这个问题和解决方案.有两个分支:with_errorfixed_error.这是diff of the change on GitHub.

Objective-c相关问答推荐

在 Objective-C 中完成受保护属性的解决方法

当 UITextField 成为第一响应者时如何使 UIScrollView 自动滚动

如何可靠地检测 iOS 9 上是否连接了外部键盘?

将 HEX NSString 转换为 NSData

UIRefreshControl iOS 6 xcode

判断正在使用的应用程序版本

ARC的正确桥接?

在 Objective-C 中在 iPhone 上使用 HTTP POST 和 GET 的教程

使用 NSMutableArray 的二维数组

MKMapView MKPointAnnotation 点击​​事件

NSString:从字符串中删除 UTF-8 重音的简单方法?

Select 时如何更改 UITableViewCell 的 colored颜色 ?

iOS NSLayoutConstraint 使用 constraintWithItem 固定宽度

从昨天开始无法将存档上传到应用store

如何以编程方式移动 UIScrollView 以集中在键盘上方的控件中?

如何在 Objective C 中将浮点数转换为 int?

UICollectionView 更新单个单元格

在 UITableView 中更改复选标记的 colored颜色

NSString 中子字符串的出现次数?

如何使用图像和标签制作自定义 UIBarButtonItem?