在 UICollectionView 中,单元格不会位于底部 [英] In a UICollectionView, the cells will not sit on the bottom

查看:19
本文介绍了在 UICollectionView 中,单元格不会位于底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Note it turned out it was a simple mistake the cells would not "sit on the bottom". However, it's an interesting question: "how to move the cells up and down." Below a brute force solution: writing your own flow layout.

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

I was amazed that the only way to do it is entirely subclass UICollectionViewFlowLayout and write some tricky code in layoutAttributesForElementsInRect.

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

It's possible this will help someone in the future, as there is very little example code for layoutAttributesForElementsInRect around. Hope it helps.

--

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

It turns out the reason my cells would not sit on the bottom was that I previously had this in my 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
}

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

of course, when I changed to a "full-view cell size" (one cell per screen), that code fails.

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

I have a simple UICollection view which is the width of an iPhone and about 250 high. The cells are the exact same size as the collection view.

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

The cells will not center, they always sit high. (i.e., outside the view.)

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

My collection view "sizes" in storyboard are all just zero. What could be the problem?

推荐答案

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

Here's one "brute force" way to solve the problem -- write your own 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 的方式基本上是这样的......

The way you "set" a UICollectionViewFlowLayout is basically like this...

class YourFancyDisplay: UICollectionViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

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

这篇关于在 UICollectionView 中,单元格不会位于底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆