CollectionView项目隐藏在顶部栏的后面

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

CollectionView项目隐藏在顶部栏的后面

从图像中我们可以看到有一条消息(实际上顶部有5条消息),但是我必须拖动集合视图以查看这些消息。我以为集合视图的尺寸实际上比屏幕大,但是collectionView.frame.heightUIScreen.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
    }

我在做什么错了?

wvw1986 回答:CollectionView项目隐藏在顶部栏的后面

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

例如,如果您不希望视图控制器出现任何障碍,请在viewWillAppear

中使用它
override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)       
        self.edgesForExtendedLayout = [] 
}
,

约束似乎是问题所在,如果您使用自定义导航栏,则应该参考约束。如果使用默认导航控制器,则可能需要将安全区域的顶部限制设为44。

但是UIScreen.main.bounds.height由完整的屏幕组成,包括顶部导航栏和20像素状态栏。对于collectionView.frame.height,您需要减去这些值。

本文链接:https://www.f2er.com/3083475.html

大家都在问