A huge glitch occurs during tableView scrolls to top

Viewed 100

Please help and give me an advise about my problem. Hello guys! I have navigationBar (top white area) + custom HeaderView (orange area) + TableView (green/yellow area) While scrolling table up my customHeaderView should be scrolling too, here I set topConstaintAnchor to that customHeaderView and moving it while moving tableView on offset.y value. The problem I cannot scroll more when current contentOffset is reach the size of customHeaderLabel and start to glitch pretty much. Sometimes it works perfectly fine but sometimes it doesn't. What have I done wrong here? And how can I fix it?

Here is a picture of my problem:

Here is the short video of my problem: https://drive.google.com/file/d/16fSaWoMj2hdylSIxcrLeK3dEOS8UPf9t/view?usp=sharing

And here is a video how it should works correctly: https://drive.google.com/file/d/1LmpLW08b7dy0OuTEEaCHHs8koKn1BYMM/view?usp=sharing

Here is my code:

// For HeaderLabel
titleLabelTopConstraint = headerLabel.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor)
headerLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 15),
headerLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -15),
headerLabel.bottomAnchor.constraint(equalTo: tableView.topAnchor)

// For TableView
tableView.topAnchor.constraint(equalTo: headerLabel.bottomAnchor),
tableView.leadingAnchor.constraint(equalTo: leadingAnchor),
tableView.trailingAnchor.constraint(equalTo: trailingAnchor),
tableView.bottomAnchor.constraint(equalTo: bottomAnchor),

// Delegate
open func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        guard UIScreen.type != .extraSmall else { return }

        let titleHeight = headerLabel.bounds.height

        if scrollView.contentOffset.y <= 0 {
            // Title is fully visible
            titleLabelTopConstraint.constant = 0
        } else if scrollView.contentOffset.y >= titleHeight {
            // Title is not visible at all
            titleLabelTopConstraint.constant = -titleHeight
        } else {
            // Title is not fully hidden or shown
            titleLabelTopConstraint.constant = -scrollView.contentOffset.y
        }
    }
0 Answers
Related