How to use collectionview flow layout to get corre

2019-06-10 19:28发布

I am using this code to get correct type but not getting the view what i want can any one tell me where am i wrong

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    screenSize = UIScreen.main.bounds
    screenWidth = screenSize.width
    screenHeight = screenSize.height
    videosCollectionView.delegate = self
    videosCollectionView.dataSource = self
}

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = videosCollectionView.dequeueReusableCell(withReuseIdentifier: "Videos", for: indexPath as IndexPath) as! VideosCollectionViewCell
    cell.mainImgVw.image = logoImage[indexPath.row]
    cell.durationLabel.text = "duration"
    cell.nameLabel.text = "Test Video"
    return cell

}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let xPadding = 10
    let spacing = 10
    let rightPadding = 10
    let width = (CGFloat(UIScreen.main.bounds.size.width) - CGFloat(xPadding + spacing + rightPadding))/2
    let height = CGFloat(215)

    return CGSize(width: width, height: height)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsetsMake(10, 10, 10, 10)
}

what i want

and what i am getting this i am getting

Please tell me where i am wrong. Please help me.

My storyboard is like this enter image description here

4条回答
Juvenile、少年°
2楼-- · 2019-06-10 19:44

Try this :-

 func collectionView(_ collectionView: UICollectionView,
                layout collectionViewLayout: UICollectionViewLayout,
                sizeForItemAt indexPath: IndexPath) -> CGSize {

      return CGSize(width: (self.view.frame.width - 8) / 3.0 , height: (self.view.frame.width - 8) / 3.0)
 }

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {

    return UIEdgeInsetsMake(8, 8, 0, 8)
}

Hope it will help you

查看更多
做个烂人
3楼-- · 2019-06-10 19:44

Declare UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout

Set min space for cell and for line from storyboard like this.

enter image description here

Set collectionview cell width like this,

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    var cellWidth:CGFloat = (collectionView.frame.size.width - 20) / 2;  

    return CGSize(width: cellWidth, height: 215);
}
查看更多
我想做一个坏孩纸
4楼-- · 2019-06-10 19:54

As you have told that you want to use collectionViewFlowDelegateLayout. So, you have to set all values 0 from storyboard as I have shown below.

enter image description here

Then you have to write this code in your viewcontroller class.

This method is used for set the cell size in collection view.

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let xPadding = 10
        let spacing = 10
        let rightPadding = 10
        let width = (CGFloat(UIScreen.main.bounds.size.width) - CGFloat(xPadding + spacing + rightPadding))/2
        let height = CGFloat(215)

        return CGSize(width: width, height: height)
    }

This method is used for the margins to apply to content in the specified section.

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsetsMake(10, 10, 10, 10)
    }

This method is used for spacing between successive rows or columns of a section.

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }
查看更多
劳资没心,怎么记你
5楼-- · 2019-06-10 19:58

Your code works fine and there isn't any problem, check Min Spacing in Size Inspector tab it should be too large, set it 10

查看更多
登录 后发表回答