简体   繁体   English

如何将集合视图单元格设置为所有屏幕的相等行单元格

[英]how to set collection view cell as equal row cell for all screen

i am using swift 2.o collection view. 我正在使用swift 2.o集合视图。 In that every thing is working fine. 这样一来,一切正常。 But when i run in different screen , the width gap between each cell are different. 但是,当我在不同的屏幕上运行时,每个单元格之间的宽度差距是不同的。 I need three cell in each row of collection view with 8 px gap for left,right,top,bottom for all cell in collection view. 我在集合视图的每一行中需要三个单元格,集合视图中的所有单元格的左,右,上,下分别有8像素的间距。 And also i need to set the cell as corner radius. 而且我还需要将单元格设置为拐角半径。 i need like below image : [![enter image description here][1]][1] 我需要下面的图片:[![在此处输入图片描述] [1]] [1]

But when i run in 5s screen i am getting like this and for 6 screen like 2nd image : 但是当我在5s屏幕上运行时,我会像这样,而对于6屏幕就像第二张图片一样:

[![enter image description here][2]][2] [![在此处输入图片描述] [2]] [2]

iphone 6 : iPhone 6 :

[![enter image description here][3]][3] [![在此处输入图片描述] [3]] [3]

See the gap between left, right, bottom , top are not equally sapced as like my first image. 看到左,右,底部,顶部之间的间隙不像我的第一幅图像那样均匀地缩小。 i need to get like my first image. 我需要像我的第一张照片一样。

I set the min line space for cell is 6 in storyboard. 我在情节提要中将单元格的最小行空间设置为6。 But i not able to get like my first imge. 但我无法像我的初恋一样。

please help me out. 请帮帮我。

my popvc.swift : 我的popvc.swift:

import UIKit

class popVC: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

    var tableData: [String] = ["RESTAURANTS", "BANKS", "COFFEE","PIZZA", "HOTELS", "TAXI", "RESTAURANTS", "BANKS","COFFEE","PIZZA", "HOTELS", "TAXI","RESTAURANTS", "BANKS", "COFFEE","PIZZA", "HOTELS", "TAXI"]
    var tableImages: [String] = ["img1.png", "img2.png", "img3.png", "img4.png", "img5.png", "img6.png", "img7.png", "img8.png", "img9.png", "img10.png", "img11.png", "img11.png", "img1.png", "img2.png", "img3.png", "img4.png", "img5.png", "img6.png"]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return tableData.count
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell: colvwCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! colvwCell
        cell.lblCell.text = tableData[indexPath.row]
        cell.imgCell.image = UIImage(named: tableImages[indexPath.row])
        return cell
    }

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        print("Cell \(indexPath.row) selected")
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

From the storyboard set CollectionViews Section Inset from size inspector as Top = 8, Bottom = 8, Left = 8 and Right = 8, it should look like as 在故事板中,将大小检查器的“集合视图”部分的“插入”设置为“顶部= 8”,“底部= 8”,“左= 8”和“右= 8”,其外观应类似于

在此处输入图片说明

And return size for cell as 并返回单元格的大小为

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

   //If you want your cell should be square in size the return the equal height and width, and make sure you deduct the Section Inset from it.
   return CGSizeMake((self.view.frame.size.width/3) - 16, (self.view.frame.size.width/3) - 45);
}

Thats it, Nothing more. 就是这样,仅此而已。 You should see the result as mine: 您应该将结果视为我的:

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

Create a collection view using following code: 使用以下代码创建集合视图:

@IBOutlet var collectionView: UICollectionView?
    var screenSize: CGRect!
    var screenWidth: CGFloat!
    var screenHeight: CGFloat!

 override func viewDidLoad() {


        print("select block is \(self.strBlockname)")

        screenSize = UIScreen.mainScreen().bounds
        screenWidth = screenSize.width
        screenHeight = screenSize.height
        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0)
        layout.itemSize = CGSize(width: (screenWidth/3)-1, height: (screenWidth/3)-1)
        layout.minimumInteritemSpacing = 1
        layout.minimumLineSpacing = 1
        collectionView = UICollectionView(frame: CGRectMake(0, 0, screenWidth, screenHeight-65), collectionViewLayout: layout)
        collectionView!.dataSource = self
        collectionView!.delegate = self
        collectionView!.registerClass(CollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewCell")
        collectionView!.backgroundColor = UIColor.whiteColor()
        self.view.addSubview(collectionView!)

        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

 func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 20
    }





    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as! CollectionViewCell
        cell.backgroundColor = UIColor.blackColor()
        cell.textLabel?.text = "\(indexPath.section):\(indexPath.row)"
        cell.imageView?.image = UIImage(named: "circle")
        return cell
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

Its Output for iPhone5s,6s and 6+ is following: 其针对iPhone5s,6s和6+的输出如下:

iPhone5s iPhone 5S

在此处输入图片说明

iPhone6s iPhone6s

在此处输入图片说明

@user5513630 Add following code in collection view. @ user5513630在集合视图中添加以下代码。

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        let size = UIScreen.mainScreen().bounds.size

// 8 - space between 3 collection cells
// 4 - 4 times gap will appear between cell.
        return CGSize(width: (size.width - 4 * 8)/3, height: 40)

    }

read this tutorial. 阅读教程。 it will fulfill your requirement 它将满足您的要求

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

相关问题 如何设置集合视图单元格大小与iOS中的集合视图完全相同? - How to set the collection view cell size exactly equal to the collection view in iOS? 如何在表格视图单元格中放置集合视图单元格(适用于所有屏幕尺寸?) - how to fit collection view cell in table view cell ( adaptive on all screen sizes?) 如何设置集合视图单元格在单行中两个单元格之间没有间隙 - How to set collection view cell without gap between two cell in single row 如何在集合视图单元格中设置多个图像? - How to set multiple images in collection view cell? 如何为每个单元格设置自定义“收集视图”单元格间距 - How to set custom Collection View Cell spacing for every cell 相对于屏幕尺寸快速设置收藏夹视图单元格的尺寸 - Set collection view cell size relative to screen size swift 集合视图的单元格未显示在行中 - cell of collection view not displaying in row 如何在选择tableView的行单元格(作为集合视图单元格)时显示视图控制器? - How to show view controller on selecting row cell of tableView(as a collection view cell)? 如何在iOS Swift中设置特定索引的集合视图单元格? - How to set collection view cell of specific index in iOS Swift? 如何在集合视图单元格Swift中检测所有OTP文本字段是否已填充 - How to detect all OTP textfields are filled or not in Collection view cell Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM