Hiding/Showing UINavigationBar 导致 UICollectionView 改变它的框架

Hiding/Showing UINavigationBar causes UICollectionView to change it's frames

我有 2 个 UIViewController,都连接到同一个 UINavigationController。我想隐藏第一个控制器的导航栏,但为第二个控制器显示它。对于此行为,我使用此代码:

override func viewWillAppear(animated: Bool) {
    self.navigationController?.setNavigationBarHidden(true, animated: false)
}

override func viewWillDisappear(animated: Bool) {
    self.navigationController?.setNavigationBarHidden(false, animated: false)
}

这会导致第二个控制器的 UICollectionView 在大约 0.5 秒后向下移动并出现黑色区域​​。灰色区域是集合视图,其顶部约束链接到包含 UISegmentedControl 的视图。

控制台中的消息:

the behavior of the UICollectionViewFlowLayout is not defined because:
2016-03-15 17:26:58.829 Goku[56059:15539317] the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.
2016-03-15 17:26:58.830 Goku[56059:15539317] The relevant     UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout:   0x7ffa28d49840>, and it is attached to <UICollectionView: 0x7ffa2a971000;   frame = (0 119; 375 548); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x7ffa28d5f950>; layer = <CALayer: 0x7ffa28d6e120>; contentOffset: {375, -64}; contentSize: {1125, 548}>   collection view layout: <UICollectionViewFlowLayout: 0x7ffa28d49840>.
2016-03-15 17:26:58.830 Goku[56059:15539317] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.

仅当我 hide/show 导航栏时才会出现此消息,如果我不这样做,自动版式也能正常工作。

将 UINavigationBar 的半透明 属性 设置为 false 出于某种原因解决了这个问题,有没有办法在不改变半透明 属性 的情况下解决这个问题?

您应该尝试取消选中 ViewController 的 "Adjust Scroll View Inset" 复选框。 打开 Storyboard,select ViewController,转到 Attributes Inspector 并取消选中 "Adjust Scroll View Inset"。