Search code examples
iosswiftchatcollectionview

CollectionView items are hidden behind top bar


I have a messaging application, the problem is that the first messages are not visible, when I drag the collection view to see the top messages I can see them but when I release the finger, the collection view jumps back, hiding the first 5 messages that are at the top of the screen

enter image description here

As we can see from the image there is a message (actually are 5 messages at the top), however I have to drag the collection view to peek those messages. I thought that the size of the collection view is actually larger that the screen, however both the collectionView.frame.height and the UIScreen.main.bounds.height have the same height, is that ok? . Here's the code that I use to setup the collection view:

 /// 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
    }

What am I doing wrong?


Solution

  • As of iOS 7.0, all views automatically go behind navigation bars, toolbars and tab bars to provide what Apple calls "context".

    For example, if you don't want a view controller to go behind any bars, use this in viewWillAppear

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