ios - 如何使用collectionview流布局来获得正确的 View ?

标签 ios swift uicollectionview uicollectionviewcell uicollectionviewflowlayout

我使用此代码来获取正确的类型,但没有获得我想要的 View ,任何人都可以告诉我哪里错了

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

我得到了什么 this i am getting

请告诉我哪里错了。请帮助我。

我的 Storyboard是这样的enter image description here

最佳答案

正如您所说,您想使用 collectionViewFlowDelegateLayout。因此,您必须将 Storyboard 中的所有值设置为 0,如下所示。

enter image description here

然后您必须在 View Controller 类中编写此代码。

该方法用于设置 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)
    }

此方法用于将边距应用于指定部分的内容。

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

此方法用于节中连续行或列之间的间距。

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }

关于ios - 如何使用collectionview流布局来获得正确的 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49937074/

相关文章:

ios - Swift字符串公式转化为真正的计算

ios - 全长 URL "edit"使用 NSMakeRange

ios - Digits.sharedInstance() 在设备上崩溃

Swift:如何避免在所有继承类中重写这个 init()?

arrays - Swift:使用替代比较对数组进行排序

swift - 如何使用swift中的计数数组移动到下一个对象?

ios - 如何将 NSString 传递给调用 printf 的函数?

iOS 路由应用注册

ios - 如何使用 MVVM 和 RxSwift 编辑/删除 UICollectionView 单元格

ios - 具有动态和静态单元格的 UICollectionView