【问题标题】:CollectionView items are hidden behind top barCollectionView 项目隐藏在顶栏后面
【发布时间】:2019-11-18 05:17:14
【问题描述】:

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

正如我们从图像中看到的那样,有一条消息(实际上顶部有 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
    }

我做错了什么?

【问题讨论】:

    标签: ios swift chat collectionview


    【解决方案1】:

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

    例如,如果您不希望视图控制器进入任何栅栏,请在 viewWillAppear 中使用它

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-18
        • 1970-01-01
        • 1970-01-01
        • 2011-05-02
        • 2013-09-03
        • 1970-01-01
        • 1970-01-01
        • 2022-06-28
        相关资源
        最近更新 更多