UICollectionView - 不在内部滚动,而是在整个屏幕中滚动

问题描述 投票:0回答:1

我正在用 Swift 构建我的第一个应用程序。我有

RestaurantViewController
。屏幕的一半被餐厅名称、描述、标志、atd 填满。 在这下面我有
UICollectionView()
,里面装满了餐厅优惠券。一切正常,但我想禁用在
CollectionView
内滚动,并允许在整个页面中滚动 - 因此当用户滚动优惠券时,整个页面会向下滚动并显示下一个优惠券。

我该怎么做?

IMAGE - Screen

IMAGE - I wanna this after scroll

优惠券是通过 API 加载的,因此在 setupUI() 等之后不久即可使用。

我的 setupUI() 内的代码:

...
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
self.couponsCollectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
couponsCollectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: CouponCollectionCell.reuseIdentifier)
couponsCollectionView!.alwaysBounceVertical = true
couponsCollectionView!.isScrollEnabled = false
...

我没有使用 Storyboard,只是以编程方式使用。还带有 UIKit 和 SnapKit(外部库) 我的 SnapKit 代码用于在屏幕中创建约束:

...
couponsCollectionView!.snp.makeConstraints { make in
   make.top.equalTo(couponsTitle.snp.bottom).offset(0)
   make.left.equalTo(0)
   make.width.equalToSuperview()
   make.leading.trailing.bottom.equalToSuperview()
}
...

谢谢!!!

ios swift uikit snapkit
1个回答
0
投票

您可以首先添加 UICollectionView 顶部的内容作为标题,其余部分作为 UICollectionView 的单元格

示例如下

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    let headerHeight: CGFloat = 300.0
    let cellHeight: CGFloat = 50.0
    var data = []

    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.showsVerticalScrollIndicator = false
        return collectionView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(collectionView)
        collectionView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            collectionView.topAnchor.constraint(equalTo: view.topAnchor),
            collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
        ])

        collectionView.register(DataCell.self, forCellWithReuseIdentifier: "DataCell")

        collectionView.register(HeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "HeaderView")
    }

    // MARK: - UICollectionViewDataSource

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 2 
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return section == 0 ? 1 : data.length
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if indexPath.section == 0 {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HeaderView", for: indexPath) as! HeaderView
// do some stuff here
            return cell
        } else {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DataCell", for: indexPath) as! DataCell
// Do some stuff here in your cell
            return cell
        }
    }

    // MARK: - UICollectionViewDelegateFlowLayout

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionView.bounds.width, height: indexPath.section == 0 ? headerHeight : cellHeight)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return section == 0 ? CGSize(width: collectionView.bounds.width, height: headerHeight) : CGSize.zero
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.