为什么只显示了一半的集合视图?

Why is only half of a collection view getting displayed?

我用过MMPlayerView(GitHublinkhere) to add videos into a collection view, in the collection view the image gets replaced by a video in the code (from the demo). I took the MMPlayerView's demo project and changed stuff to fit my needs and then implemented it into my project to get something like this show in this video

现在我想明显地将集合视图向上移动到屏幕顶部的按钮,稍后我将使用多个集合视图来获得更像应用程序商店的样式,只是显示视频。因为我从演示中获取了代码,所以我对某些事情不太确定,当我复制项目的演示时,集合视图是为了垂直滚动它而制作的,所以它占据了整个屏幕,所以集合视图被限制为视图底部:

为什么它会显示在应用程序中(我不明白为什么它在受到 10 点限制时离顶部那么远):

现在我显然想删除底部约束并通过它的高度来约束它,这样我就可以有多个集合视图,(可能通过使用 table 视图),所以我删除了约束到视图底部并添加 250 点的高度约束:

这导致应用程序看起来像这样:

为什么??我猜它与视图控制器代码的这一部分有关:

extension HomeViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let m = min(UIScreen.main.bounds.size.width, UIScreen.main.bounds.size.height)
    return CGSize(width: m, height: m*0.75)
}

我想保持集合视图的宽度与视图的宽度相同,所以宽度可以,这里只是高度。当我尝试将高度设置为 250 的常量值(与高度限制相同)时,它会做同样的事情(只显示一半)但会稍微缩小视频。我能改变什么?

快速浏览 MMPlayerView 中的示例应用...

sizeForItemAt 中的高度值更改为您的 collection 视图的高度(250 根据您的问题):

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let m = min(UIScreen.main.bounds.size.width, UIScreen.main.bounds.size.height)
    return CGSize(width: m, height: 250.0)
    //return CGSize(width: m, height: m*0.75)
}

我假设您在调试控制台中收到的错误消息与我收到的错误消息相同:

The behavior of the UICollectionViewFlowLayout is not defined because:

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.

它还应该打印 Collection 视图属性,包括:

adjustedContentInset: {0, 0, 200, 0};

因此 collection 视图的底部插图为 200。

如果您在该示例项目中搜索 contentInset,您将在视图控制器的 viewDidLoad() 函数中找到这一行:

playerCollect.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 200, right:0)

如果您 comment-out / 删除该行,您现在应该会看到完整的单元格。

开始处理 collection 视图可能是个好主意,然后 然后 查看该示例项目以在您的应用中实现视频。