首页 > 解决方案 > CollectionView 项目隐藏在顶栏后面

问题描述

我有一个消息传递应用程序,问题是第一条消息不可见,当我拖动集合视图以查看顶部消息时,我可以看到它们,但是当我松开手指时,集合视图跳回来,隐藏了前 5 条消息位于屏幕顶部

在此处输入图像描述

正如我们从图像中看到的那样,有一条消息(实际上顶部有 5 条消息),但是我必须拖动集合视图才能查看这些消息。我认为集合视图的大小实际上比屏幕大,但是两者的collectionView.frame.height高度UIScreen.main.bounds.height相同,可以吗?. 这是我用来设置集合视图的代码:

 /// Function that configures the ChatViewController collection view
    private func configureCollectionView() {
        collectionView?.backgroundColor = Theme.current.chatGeneralBackground
        collectionView?.alwaysBounceVertical = true
        collectionView?.keyboardDismissMode = .onDrag

        // Register the chat cell and the loading cellx`
        collectionView?.register(ChatCell.self, forCellWithReuseIdentifier: chatCellIdentifier)
        collectionView?.register(LoadingCollectionViewCell.self, forCellWithReuseIdentifier: loadingCellIdentifier)

        // Initialize the original height of the collection view
        collectionViewOriginalHeight = collectionView.frame.height
    }

我究竟做错了什么?

标签: iosswiftchatcollectionview

解决方案


从 iOS 7.0 开始,所有视图都自动位于导航栏、工具栏和标签栏后面,以提供 Apple 所谓的“上下文”。

例如,如果您不希望视图控制器位于任何栏后面,请在viewWillAppear

override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)       
        self.edgesForExtendedLayout = [] 
}

推荐阅读