简体   繁体   English

如何将 colectionViewCells 与左侧对齐?

[英]How Can I align the colectionViewCells to the left?

I have ac UIolectionView that sometimes has only 1 item.我有一个有时只有 1 个项目的 ac UIolectionView If that is the case, the item is aligned in the middle, see picture:如果是这种情况,则项目在中间对齐,请参见图片:

在此处输入图片说明

But what I want, is that the item is aligned to the left.但我想要的是该项目与左侧对齐。

I have found this answer on StackOverflow, leftAlign cells in UIColectioniew - StackOverFlow , but when I added the class given in the accepted answer to my codeBase, and added this to the viewControler :我在 StackOverflow 上找到了这个答案, UICollectioniew 中的 leftAlign 单元格 - StackOverFlow ,但是当我将接受的答案中给出的类添加到我的代码库时,并将其添加到viewControler

        let leftAlignLayout = AlignedCollectionViewFlowLayout(horizontalAlignment: .left, verticalAlignment: .top)
        gamesColectionView.collectionViewLayout = leftAlignLayout

it did align the way I wanted, but it made the cells very small(see picture)它确实按照我想要的方式对齐,但它使细胞非常小(见图)

在此处输入图片说明

I also tried to add this from Github: AlignedCollectionViewFlowLayout - GitHub but that had the same result.我还尝试从 Github 添加此内容: AlignedCollectionViewFlowLayout - GitHub但结果相同。

I tried fixing that bug by adding this to my viewController File:我尝试通过将其添加到我的viewController文件来修复该错误:

func collectionView(_ collectionView: UICollectionView,
           layout collectionViewLayout: UICollectionViewLayout,
           sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 130, height: 130)
    }

but that didn't work.但这没有用。

does anybody have a suggestion on how I can fix this?有人对我如何解决这个问题有建议吗? I don't know what I did wrong or what I forgot to do.我不知道我做错了什么,或者我忘了做什么。

thanks a lot!多谢! BSM BSM

Try this:尝试这个:

var layout: UICollectionViewFlowLayout = {
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .horizontal
    layout.minimumLineSpacing = 8.0
    layout.minimumInteritemSpacing = 8.0
    let width = (UIScreen.main.bounds.size.width - 40.0) / 4
    layout.estimatedItemSize = CGSize(width: width, height: 98.0)
    return layout
}()

And:和:

override func viewDidLoad() {
    super.viewDidLoad()
    self.collectionView?.collectionViewLayout = layout
    self.collectionView!.contentInset = UIEdgeInsets(top: 0, left: 0, bottom:0, right: 0)
}

You don't need to use sizeForItemAt.您不需要使用 sizeForItemAt。 If you want more you can look my project .如果你想要更多,你可以看看我的项目 Check constraints of your imageView with it's superview用它的超级视图检查你的 imageView 的约束

You can manage your cell with below code.您可以使用以下代码管理您的单元格。 Do not forget to add UICollectionViewDelegateFlowLayout不要忘记添加UICollectionViewDelegateFlowLayout

func collectionView(_ collectionView: UICollectionView,
                layout collectionViewLayout: UICollectionViewLayout,
                sizeForItemAt indexPath: IndexPath) -> CGSize {
// your code here
} 

You don't need to do anything special to get the collectionViewCells left aligned.您无需执行任何特殊操作即可使collectionViewCells左对齐。

If the constraints are proper, the cells will automatically start rendering from the left.如果constraints正确, cells将自动从左侧开始渲染。

class VC: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 1
    }

        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
            return cell
        }

        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
            return CGSize(width: 130, height: 130)
        }
    }

Simply set the scrollDirection to vertical or horizontal as per your requirement within the storyboard itself.只需根据storyboard本身的要求将scrollDirection设置为verticalhorizontal

There is no need to use any 3rd party library to get that working.无需使用任何 3rd 方库即可使其正常工作。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM