CollectionView header 状态栏下的内容
CollectionView header content under status bar
这是一个 collection 视图,它被限制在超级视图的顶部、左侧、右侧和底部,启用了 安全区域布局指南 :
我希望我的 collection 视图 header 显示在状态栏下方。我通过取消选中控制器主 view
的尺寸检查器中的 安全区域布局指南 并添加以下代码:
collectionView.contentInset = UIEdgeInsets(top: -20, left: 0, bottom: 0, right: 0)
这看起来非常适合 非 iPhone X 视图尺寸:
但是,对于 iPhone X,这会导致以下输出:
iPhone X 状态栏有自己的尺寸。进一步调整顶部插图 确实 有效,但会 over-offset 其他设备尺寸。我想知道是否有更优雅的方法来实现这种行为。
找到解决方案:
collectionView.contentInset.top = -UIApplication.shared.statusBarFrame.height
添加 2 个约束:
1) 视图-超级视图
2) 查看 - 安全区域
您应该为 iphone X
使用 safeAreaInsets
if #available(iOS 11.0, *) {
if 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
}
以前的解决方案有效,但这可能是最简单的解决方案:
collectionView.contentInsetAdjustmentBehavior = .never
这样就可以了
collectionView.contentInsetAdjustmentBehavior = .never
这是一个 collection 视图,它被限制在超级视图的顶部、左侧、右侧和底部,启用了 安全区域布局指南 :
我希望我的 collection 视图 header 显示在状态栏下方。我通过取消选中控制器主 view
的尺寸检查器中的 安全区域布局指南 并添加以下代码:
collectionView.contentInset = UIEdgeInsets(top: -20, left: 0, bottom: 0, right: 0)
这看起来非常适合 非 iPhone X 视图尺寸:
但是,对于 iPhone X,这会导致以下输出:
iPhone X 状态栏有自己的尺寸。进一步调整顶部插图 确实 有效,但会 over-offset 其他设备尺寸。我想知道是否有更优雅的方法来实现这种行为。
找到解决方案:
collectionView.contentInset.top = -UIApplication.shared.statusBarFrame.height
添加 2 个约束:
1) 视图-超级视图
2) 查看 - 安全区域
您应该为 iphone X
使用 safeAreaInsetsif #available(iOS 11.0, *) {
if 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
}
以前的解决方案有效,但这可能是最简单的解决方案:
collectionView.contentInsetAdjustmentBehavior = .never
这样就可以了
collectionView.contentInsetAdjustmentBehavior = .never