状态栏下的CollectionView标题内容

时间:2018-02-22 19:56:33

标签: ios uicollectionview iphone-x safearealayoutguide

这是一个限制在超级视图顶部,左侧,右侧和底部的集合视图,其中启用了安全区域布局指南

enter image description here

我希望我的收藏视图标题在状态栏下显示。我通过取消选中控制器主view的尺寸检查器中的安全区域布局指南,并添加以下代码来实现iPhone 4 - 8+屏尺寸:

collectionView.contentInset = UIEdgeInsets(top: -20, left: 0, bottom: 0, right: 0)

这对非iPhone X 视图尺寸非常有用:

enter image description here

但是,对于iPhone X,这会产生以下输出:

enter image description here

iPhone X有自己的状态栏尺寸。进一步调整顶部插入 可以正常工作,但会过度偏移其他设备尺寸。我想知道是否有一种更优雅的方式来实现这种行为。

5 个答案:

答案 0 :(得分:3)

找到解决方案:

collectionView.contentInset.top = -UIApplication.shared.statusBarFrame.height

答案 1 :(得分:1)

添加2个约束:

1)view - superview

enter image description here

2)view - safeArea

enter image description here

答案 2 :(得分:1)

你应该对iphone X使用safeAreaInsets

if #available(iOS 11.0, *) {
    let top = UIApplication.shared.keyWindow?.safeAreaInsets.top
    collectionView.contentInset = UIEdgeInsets(top: -top, left: 0, bottom: 0, right: 0)

} else {
    // Fallback on earlier versions
    collectionView.contentInset.top = -UIApplication.shared.statusBarFrame.height
}

答案 3 :(得分:1)

这个就行了

collectionView.contentInsetAdjustmentBehavior = .never

答案 4 :(得分:0)

先前的解决方案有效,但这可能是最简单的解决方案:

collectionView.contentInsetAdjustmentBehavior = .never