如何使用 collectionview 流布局来获得正确的视图? [英] How to use collectionview flow layout to get correct view?

查看:24
本文介绍了如何使用 collectionview 流布局来获得正确的视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码来获取正确的类型,但没有获得我想要的视图,谁能告诉我我错在哪里

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)
}

我得到了什么

请告诉我我错在哪里.请帮帮我.

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

我的故事板是这样的

推荐答案

正如您所说,您要使用 collectionViewFlowDelegateLayout.因此,您必须将故事板中的所有值设置为 0,如下所示.

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.

然后你必须在你的视图控制器类中编写这段代码.

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
    }

这篇关于如何使用 collectionview 流布局来获得正确的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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