首页 文章

UIViewController里面的UICollectionView不允许滚动

提问于
浏览
1

Edit1: I am not using a storyboard everything has been added programmatically
Edit2: Updated code snippet

您好,我对UICollectionView有以下问题

  • The CollectionView won't allow you to scroll no matter what you do.

  • Sometimes the cells even disappear complete leaving you with a white background CollectionView.

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

  • 我创建了一个具有不同类型单元格的CollectionView .

  • CollectionView嵌套在ImageView下面的ViewController中 .

  • 添加了约束,它们工作得很好 .

如何使其可滚动?

GIF representing the problem

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 回答

  • 1

    Heyy你告诉的是你无法向上滚动并查看整个集合查看项目?

    如果是这种情况,那么你没有考虑tabbar控制器的高度,所以你可以启用半透明,例如我有一个git repo我让你可以检查,让我知道你是否被困在任何地方

    https://github.com/dwivediashish00/socialApp

相关问题