【问题标题】:In a UICollectionView, the cells will not sit on the bottom在 UICollectionView 中,单元格不会位于底部
【发布时间】:2023-03-03 13:57:01
【问题描述】:

注意原来这是一个简单的错误,细胞不会“坐在底部”。然而,这是一个有趣的问题:“如何上下移动细胞。”下面是一个蛮力解决方案:编写自己的流程布局。

我很惊讶,唯一的方法是完全继承 UICollectionViewFlowLayout 并在 layoutAttributesForElementsInRect 中编写一些棘手的代码。

这可能会在将来对某人有所帮助,因为 layoutAttributesForElementsInRect 的示例代码很少。希望对您有所帮助。

--

事实证明,我的单元格不会位于底部的原因是因为我之前在我的 UICollectionViewController 中有这个...

-(void)viewDidLoad
{
[super viewDidLoad];
// the following code is very useful to, for example,
// "nudge short lists towards the middle..." - if you want the list
// to more sit in the middle of the screen, when only a few items.
cvHeight = CGRectGetHeight(self.collectionView.bounds);
[self.collectionView setContentInset:
       UIEdgeInsetsMake(cvHeight * 0.175, 0, cvHeight * 0.30, 0) ]; //top,l,b,r
}

当然,当我更改为“全视图单元格大小”(每个屏幕一个单元格)时,该代码会失败。


我有一个简单的 UICollection 视图,它是 iPhone 的宽度和大约 250 高。单元格与集合视图的大小完全相同。

细胞不会居中,它们总是坐得很高。 (即,在视图之外。)

我在情节提要中的收藏视图“大小”都为零。可能是什么问题?

【问题讨论】:

  • 检查您的收集单元,这可能是问题所在。
  • 检查单元格上的自动调整大小或在 CollectionView 上添加部分插图(顶部)
  • 单元格上的自动调整大小无关。设置 section insets 似乎一无所获 - 没有变化!
  • 确保您没有从主线程外部更新集合视图,或者以可能导致它在更新中更新的方式更新,等等。我有一些奇怪的行为,我可以追溯到那种东西。
  • 嗨,Caleb - 谢谢;绝对没有线程问题。不过谢谢 :O

标签: ios uicollectionview uicollectionviewcell


【解决方案1】:

这是解决问题的一种“蛮力”方法——编写自己的 FlowLayout!

它有效...

class SimpleFlowLayout: UICollectionViewFlowLayout {


override init() {
    super.init()

    initialize()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    initialize()
}

func initialize() {
    self.scrollDirection = .horizontal
    self.minimumInteritemSpacing = 0.0
    self.minimumLineSpacing = 0.0

}

override var collectionViewContentSize: CGSize {

    return super.collectionViewContentSize
}

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {


    let allItems = super.layoutAttributesForElements(in: rect)
    for  attribute in allItems! {
        print ("attribute.frame.origin.y  .....  \(attribute.frame.origin.y)")
        attribute.frame = CGRect(x: attribute.frame.origin.x,
                                 y: attribute.frame.origin.y + 34,
                             width: attribute.frame.size.width,
                            height: attribute.frame.size.height)

            // "go figure" ... add 34
    }

    return allItems;

}

你“设置”一个 UICollectionViewFlowLayout 的方式基本上是这样的......

class YourFancyDisplay: UICollectionViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView!.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
        collectionView!.collectionViewLayout = SimpleFlowLayout()
        ...

【讨论】:

  • 我只是勾选了这个,因为它可以帮助带有流布局示例代码的人。这几乎是上下移动它们的唯一方法!
  • @Fattie 你是如何实现 SimpleLayout 的?你在哪里使用它作为子类
  • 我理解实现。但是,我还有最后一个问题。在覆盖 func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]?看起来您为每个索引路径中的每个单元格设置了属性。如果您将两个单元格设置为与第一项不同的布局怎么办。
  • het Stef,就像在表格视图中一样,您只需决定使用哪个自定义单元格。请参阅任何教程 - 干杯,祝你好运
  • 我查看了文档中的 layoutAttributesForElements 并指出它:返回指定矩形中所有单元格和视图的布局属性。这意味着自定义单元格仍然符合布局。还有其他形式/版本的功能吗?
【解决方案2】:

试试这个。希望能帮到你

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return collectionView.frame.size;
}

(对于未来的读者,实际上您通常还需要另一个...)

-(CGSize) collectionView:(UICollectionView *)collectionView
     layout:(UICollectionViewLayout *)collectionViewLayout
     sizeForItemAtIndexPath:(NSIndexPath *)indexPath
    {
    return self.view.frame.size;
    }

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView
   layout:(UICollectionViewLayout*)collectionViewLayout
   insetForSectionAtIndex:(NSInteger)section
    {
    return UIEdgeInsetsMake(0,0,0,0);   //t,l,b,r
    }

【讨论】:

  • 确实 .. 不幸的是,我正在使用它(或者它根本不起作用,嘿!)见 ..stackoverflow.com/questions/24915443 真是太糟糕了。
  • 你能在NSLog上面返回self.view.frame.size吗?只需确保框架大小更改为应有的大小即可。
  • 请注意,SIZE 是正确的——摩擦位置是错误的!!!!!!你到底是怎么设置位置的?!
  • 我想帮助你。你是选中还是取消选中 useAutoLayout?
  • 对,很狂野哈!该死的苹果! :) 我没有使用自动布局。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-07
  • 1970-01-01
  • 1970-01-01
  • 2020-02-13
相关资源
最近更新 更多