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

查看:104
本文介绍了在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...(as far as I know!)

class YourFancyDisplay: UICollectionViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

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

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

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