在UICollectionView中显示一行-Swift [英] Display one row in UICollectionView - Swift

查看:62
本文介绍了在UICollectionView中显示一行-Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何限制UICollectionView在单个行中显示水平可滚动列表.

I'm trying to figure out how to limit a UICollectionView to display a horizontal scrollable list in a single row.

有什么想法吗?

推荐答案

我刚刚使用了对我有用的东西:

I just used something that is working for me:

  1. 将滚动方向"设置为水平".
  2. 使用 sizeForItemAt indexPath 方法设置 Cell 的大小.
  3. 为您的 CollectionView 设置一个约束,使其高度等于(或稍大于)您在上面指定的 Cell 的高度.
  1. Set the Scroll direction to Horizontal.
  2. Set the size of your Cells using sizeForItemAt indexPath method.
  3. Set a constraint to your CollectionView making its height equal to (or a bit greater than) the Cell's height which you specified above.

其背后的逻辑:

CollectionView 的高度等于其 item 的高度并将其设置为水平滚动时,则只能将它们显示为一个.我说或比 cell 的高度大一点"是因为如果您将 CollectionView 的高度设置为比 cell 的高度大两倍那么它可以容纳两行 cell ,并且将它们显示为两行,而不是一列.

When the height of CollectionView is equal to the height of it's items and it is set to horizontal scroll, then it can only show them in one row. I said "or a bit greater than cell's height" because if you set the height of your CollectionView twice bigger than cell's height then it can fit 2 rows of cells and it will show them in 2 rows instead of one.

这篇关于在UICollectionView中显示一行-Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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