Ios 在UICollectionView中创建2 x 2水平网格?
我有一个带有UICollectionViewController的Popover。我试图制作一个2 x 2的网格,但它只显示一行。我要两排 我试图将UICollectionView框架除以行数,但它仍然显示一行Ios 在UICollectionView中创建2 x 2水平网格?,ios,swift,uicollectionview,xcode7,Ios,Swift,Uicollectionview,Xcode7,我有一个带有UICollectionViewController的Popover。我试图制作一个2 x 2的网格,但它只显示一行。我要两排 我试图将UICollectionView框架除以行数,但它仍然显示一行 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSI
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)
}
(注:我故意把府绸做得很大)
您需要将最小间距和截面插入值设置为零 如果要显示两个单元格之间的间距,请使用以下公式
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)
}
对于所有scrren大小,请尝试以下代码:
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);
}
我做到了
好的,所以最初我的集合视图大小是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)
我将集合视图帧大小更改为300×300(最初是768×1024)。这不起作用,因为如果我将我的popover视图更改回preferredContentSize
=CGSizeMake(300300)
,则集合视图将根本不会显示
以下是我在我的collectionView(collectionView:UICollectionView,layout collectionViewLayout:UICollectionViewLayout,sizeFormiteIndeXPath indexPath:NSIndexPath)中所做的工作
我将UICollectionView的大小保存在一个变量中,然后将宽度和高度设置为300,与我的popover的preferredContentSize
相同。这是我想要的结果:
它是一个水平滚动的网格:)我想你们应该在
numberofitemsinssection
方法中提到数字。所以我想看4部。2 x 2网格水平滚动。对行使用numberOfSectionsInCollectionView
,对每行中的元素数量使用numberofitemsinssection
。我认为您应该在视图中添加滚动视图以水平滚动。请在此方法中指定NumberOfItems在collectionview numberOfRows/2部分我认为您的答案可能已经引导我找到了我发布的解决方案!
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)
}