隐藏/显示UINavigationBar会导致UICollectionView更改其帧

时间:2016-03-16 09:02:58

标签: ios swift uicollectionview uinavigationbar

我有2个UIViewControllers,都连接到同一个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.

仅当我隐藏/显示导航栏时才会显示此消息,如果不显示,则autolayout工作正常。

将UINavigationBar的半透明属性设置为false会因某种原因修复问题,有没有办法解决此问题而不更改半透明属性?

1 个答案:

答案 0 :(得分:3)

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

enter image description here