在 UICollectionView 中创建 2 x 2 水平网格?
Creating a 2 x 2 Horizontal Grid in UICollectionView?
我有一个带有 UICollectionViewController 的 Popover。我正在尝试制作一个 2 x 2 的网格,但它只显示一行。我要两排。
我试图将 UICollectionView 框架除以行数,但它仍然显示一行。
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let itemWidth = CGRectGetWidth(collectionView.frame)
let itemHeight = CGRectGetHeight(collectionView.frame)
let squareSizeHeight = itemHeight / 2
let squareSizeWidth = itemWidth / 2
return CGSizeMake(squareSizeWidth, squareSizeHeight)
}
(注意:我故意把popover做得很大)
他需要将 最小间距 和 部分插入 值设置为零
如果您想显示两个单元格之间的间距,请使用以下公式
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let itemWidth = CGRectGetWidth(collectionView.frame) - spacingValue
let itemHeight = CGRectGetHeight(collectionView.frame) - spacingValue
let squareSizeHeight = itemHeight / 2
let squareSizeWidth = itemWidth / 2
return CGSizeMake(squareSizeWidth, squareSizeHeight)
}
为所有屏幕尺寸尝试此代码:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let screenSize:CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
return CGSize(width: (screenWidth/2) - 15, height: (screenWidth/2) - 15);
}
我做到了!
好的,原来我的 Collection 视图大小是 300 x 300(横向)。
我这样做了:
class PhoneViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
let cellId = "cellId"
override func viewDidLoad() {
super.viewDidLoad()
//collectionView?.frame = CGRectMake(0, 46, 300, 300) // A no-no here
navigationItem.title = "Recipients"
collectionView?.registerClass(UserContactCells.self, forCellWithReuseIdentifier: cellId)
collectionView?.backgroundColor = UIColor.redColor()
collectionView?.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
print(collectionView?.frame)
我将 collection 视图框架大小更改为 300 x 300(最初为 768 x 1024)。那是行不通的,因为如果我将弹出窗口视图改回 preferredContentSize
= CGSizeMake(300, 300)
,则根本不会显示 collection 视图。
这是我在 collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath)
中所做的
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
var cvSize = collectionView.frame.size
cvSize = CGSizeMake(300, 300)
let itemWidth = cvSize.width - 1
let itemHeight = cvSize.height - 1
let squareSizeHeight = itemHeight / 2
let squareSizeWidth = itemWidth / 2
return CGSizeMake(squareSizeWidth, squareSizeHeight)
}
我把我的 UICollectionView 的大小保存在一个变量中,然后将宽度和高度设置为 300,与我的弹出窗口的 preferredContentSize
相同。那是我想要的结果:
而且它是一个水平滚动的网格:)
我有一个带有 UICollectionViewController 的 Popover。我正在尝试制作一个 2 x 2 的网格,但它只显示一行。我要两排。
我试图将 UICollectionView 框架除以行数,但它仍然显示一行。
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let itemWidth = CGRectGetWidth(collectionView.frame)
let itemHeight = CGRectGetHeight(collectionView.frame)
let squareSizeHeight = itemHeight / 2
let squareSizeWidth = itemWidth / 2
return CGSizeMake(squareSizeWidth, squareSizeHeight)
}
(注意:我故意把popover做得很大)
他需要将 最小间距 和 部分插入 值设置为零
如果您想显示两个单元格之间的间距,请使用以下公式
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let itemWidth = CGRectGetWidth(collectionView.frame) - spacingValue
let itemHeight = CGRectGetHeight(collectionView.frame) - spacingValue
let squareSizeHeight = itemHeight / 2
let squareSizeWidth = itemWidth / 2
return CGSizeMake(squareSizeWidth, squareSizeHeight)
}
为所有屏幕尺寸尝试此代码:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let screenSize:CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
return CGSize(width: (screenWidth/2) - 15, height: (screenWidth/2) - 15);
}
我做到了!
好的,原来我的 Collection 视图大小是 300 x 300(横向)。
我这样做了:
class PhoneViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
let cellId = "cellId"
override func viewDidLoad() {
super.viewDidLoad()
//collectionView?.frame = CGRectMake(0, 46, 300, 300) // A no-no here
navigationItem.title = "Recipients"
collectionView?.registerClass(UserContactCells.self, forCellWithReuseIdentifier: cellId)
collectionView?.backgroundColor = UIColor.redColor()
collectionView?.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
print(collectionView?.frame)
我将 collection 视图框架大小更改为 300 x 300(最初为 768 x 1024)。那是行不通的,因为如果我将弹出窗口视图改回 preferredContentSize
= CGSizeMake(300, 300)
,则根本不会显示 collection 视图。
这是我在 collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath)
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
var cvSize = collectionView.frame.size
cvSize = CGSizeMake(300, 300)
let itemWidth = cvSize.width - 1
let itemHeight = cvSize.height - 1
let squareSizeHeight = itemHeight / 2
let squareSizeWidth = itemWidth / 2
return CGSizeMake(squareSizeWidth, squareSizeHeight)
}
我把我的 UICollectionView 的大小保存在一个变量中,然后将宽度和高度设置为 300,与我的弹出窗口的 preferredContentSize
相同。那是我想要的结果:
而且它是一个水平滚动的网格:)