UIViewController中的UICollectionView不允许滚动

时间:2018-12-10 08:12:21

标签: ios swift uiviewcontroller uicollectionview collectionview

Edit1:我没有使用情节提要,而是通过编程方式添加了所有内容
Edit2:更新了代码段

你好,我遇到了UICollectionView的以下问题

  • 无论您做什么,CollectionView都不允许您滚动。
  • 有时候,这些单元格甚至消失不见,甚至留下白色背景的CollectionView。

当您尝试向上滚动以查看不可见的单元格时,会出现问题。

  • 我创建了一个具有不同类型单元格的CollectionView。
  • CollectionView嵌套在ImageView下方的ViewController中。
  • 添加了约束,它们可以正常工作。

我如何使其可滚动?

代表问题的GIF

GIF Video

ViewController.swift

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    self.navigationItem.title = "Featured"
    self.view.backgroundColor = UIColor.white

    // Add UImage carousel
    let carousel: UIImageView = {
        let imageView = UIImageView()
        let image = UIImage()

        imageView.backgroundColor = UIColor.purple

        return imageView
    }()
    self.view.addSubview(carousel)
    carousel.translatesAutoresizingMaskIntoConstraints = false

    // Add CollectionView
    let featuredControllerLayout = UICollectionViewFlowLayout()
    // Add CollectionViewController
    featuredControllerLayout.scrollDirection = .vertical
    let featuredController = FeaturedCollectionViewController(collectionViewLayout: featuredControllerLayout)
    guard let featuredView = featuredController.collectionView else { return }

    self.view.addSubview(featuredView)
    featuredView.translatesAutoresizingMaskIntoConstraints = false

    // Setup Constraints
    if #available(iOS 11.0, *) {
        let guide = self.view.safeAreaLayoutGuide
        let guideSize = guide.layoutFrame.size

        carousel.trailingAnchor.constraint(equalTo: guide.trailingAnchor).isActive = true
        carousel.leadingAnchor.constraint(equalTo: guide.leadingAnchor).isActive = true
        carousel.topAnchor.constraint(equalTo: guide.topAnchor).isActive = true
        carousel.frame.size.height = guideSize.width/2
        carousel.heightAnchor.constraint(equalToConstant: carousel.frame.size.height).isActive = true

        featuredView.trailingAnchor.constraint(equalTo: guide.trailingAnchor).isActive = true
        featuredView.leadingAnchor.constraint(equalTo: guide.leadingAnchor).isActive = true
        featuredView.topAnchor.constraint(equalTo: carousel.bottomAnchor).isActive = true
        featuredView.bottomAnchor.constraint(equalTo: guide.bottomAnchor).isActive = true
    }
}

1 个答案:

答案 0 :(得分:1)

嘿,您要说的是您无法向上滚动并查看整个收藏夹查看项目吗?

如果是这种情况,则您尚未考虑Tabbar控制器的高度,因此您可以启用半透明功能,例如,我有一个git repo,可以检查并让我知道是否卡在任何地方

https://github.com/dwivediashish00/socialApp