滚动到CollectionView的底部时,Collection View单元会无意中调整大小

我正在建立图像的收藏夹视图(画廊)。

布局是每行3个单元格。

import matplotlib.pyplot as plt
import numpy as np
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import os,sys

conus_proj = ccrs.LambertConformal(central_longitude=-96,central_latitude=39.0)
fig = plt.figure(figsize=(10,8))
ax = fig.add_subplot(1,1,projection=conus_proj)
ax.set_extent([-120,-70,22,50])

ax.add_feature(cfeature.COASTLINE)
ax.add_feature(cfeature.OCEAN,facecolor='#CCFEFF')
ax.add_feature(cfeature.LAKES,facecolor='#CCFEFF')
ax.add_feature(cfeature.RIVERS,edgecolor='#CCFEFF')
ax.add_feature(cfeature.LAND,facecolor='#FFE9B5')
ax.add_feature(cfeature.STATES,edgecolor='black',zorder=10)

plt.plot([-120,-70],[35,45],linewidth=8,transform=ccrs.PlateCarree())
plt.show()

一切都很不错,直到我滚动到收藏夹视图的底部。

它来自: 3 per row,looks good

至: super blown up picture,larger than the screen

我也收到此错误消息:

x111 1111 1111 xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx

任何见解都会很棒,我想把它变成无限滚动(api预取),如果这意味着我可以忽略这一点,那就说吧。

caosh666666 回答:滚动到CollectionView的底部时,Collection View单元会无意中调整大小

由于所有项目的大小均相同,因此不应在委托方法中进行设置。而是在viewDidLayoutSubviews中的flowLayout上设置静态大小(您可以从情节提板拖出插座),并使用布局插图来做到这一点,这样就不会出现数学错误:

constexpr
,

您得到的错误是指向项目的宽度,这是说的问题,基本上是,项目的宽度和间距不能超过集合视图的宽度。这是因为您的收藏夹视图具有垂直滚动。

因此,要使集合视图实现正确的行为,必须将控制器设置为集合视图的delegate,并采用UICollectionViewDelegateFlowLayout。就您而言,我可以看到您已经实现了collectionView(_:,layout:,sizeForItemAt:方法。

在您的实现中,有一个明确的意图是将collectionView的宽度分成三个相等的部分。该计算正在考虑self.view.width减去八的三分之一。如果我假设正确,那么您打算将项目间的间距保留为8。如果是这种情况,则应将其指定为另一种方法:

func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 8
}

这将指定项目之间的间距为8点。

继续使用单元格的widthheight,然后必须除以collectionView.frame.width ,但是在此之前,必须从该数量中减去间隔值 ,因为那是您单元格的剩余空间。

所以您的实现应该是

func collectionView(_ collectionView: UICollectionView,sizeForItemAt indexPath: IndexPath) -> CGSize {
    // We subtract 16 because there are 2 inter item spaces like:
    // [CELL] [spacing:8] [CELL] [spacing:8] [CELL]
    let usableWidth = collectionView.frame.width - (2 * 8) 

    let cellWidth: CGFloat = usableWidth / 3
    let cellHeight: CGFloat = cellWidth

    return CGSize(width: cellWidth,height: cellHeight)
}

这应该为您做布局。

,

首先,为什么不使用 bounds 而不是 frame

第二,发生这种情况的原因很可能是因为您在collectionView加载单元格时正在其他位置调整布局(滚动将使其加载单元格)。

您是否使用UIScrollViewDelegate方法对布局进行任何操作?

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

大家都在问