Vertical scrolling with TableView inside horizontal CollectionView

Viewed 183

I have a horizontal UICollectionView (with paging) where each cell has a fullscreen UITableView. The parent ViewController has a navigation bar with large titles where an animation occurs during vertical scrolling. However, since my table views are within the horizontal collection view, the large navigation bar does not animate when scrolling.

I have attempted a solution of setting a delegate for the tableview scrolling and then set the y offset of the collection view but the result glitches and does not work correctly.

Here is my current code (without TableView/CollectionView delegate/datasource methods)

class HomeViewController: UIViewController, HomeCellDelegate {

    var delegate: HomeViewControllerDelegate!
    var collectionView: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()
        create()
    }

    func create() {
        view.backgroundColor = .background
    
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.sectionInset = .zero
        collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        view.addSubview(collectionView)
        collectionView.anchor(top: view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
        collectionView.backgroundColor = .clear
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.isPagingEnabled = true
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(HomeCell.self, forCellWithReuseIdentifier: "cellID")
    }

    //Delegate from UICollectionView cell (the table view)
    func tableViewDidScroll(_ contentOffset: CGPoint) {
        collectionView.contentOffset.y = contentOffset.y //Glitches
    }
}



protocol HomeCellDelegate {
     func tableViewDidScroll(_ contentOffset: CGPoint)
}
class HomeCell: UICollectionViewCell {

    var events = [String]()

    let tableView = UITableView()
    var delegate: HomeCellDelegate!

    override init(frame: CGRect) {
        super.init(frame: frame)
        create()
    }

    func create() {
        backgroundColor = .clear
    
        addSubview(tableView)
        tableView.anchor(top: topAnchor, left: leftAnchor, bottom: bottomAnchor, right: rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
        tableView.contentInsetAdjustmentBehavior = .never
    }

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let offset = CGPoint(x: 0.0, y: scrollView.contentOffset.y + scrollView.adjustedContentInset.top)
        delegate.tableViewDidScroll(offset)
    }

    required init?(coder: NSCoder) {
        fatalError()
    }
}
0 Answers
Related