0
votes

I have implemented scrollViewDidScroll so that a header view (pagerView) will collapse as the user scrolls the scrollView. It works perfectly if the content of the scrollView is long enough so that it scrolls off the top of the screen. But, if there is less content, the scrollView will stick, i.e. stop scrolling once it reaches its bottom, and not allow scrolling back down. Any help would be fantastic.


let pagerViewMaxHeight = 350

let pagerViewMinHeight = 44 + statusBarHeight

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let y = scrollView.contentOffset.y
        let newPagerViewHeight = pagerViewHeight.constant - y

        if newPagerViewHeight > pagerViewMaxHeight {
            pagerViewHeight.constant = pagerViewMaxHeight
        } else if newPagerViewHeight < pagerViewMinHeight {
            pagerViewHeight.constant = pagerViewMinHeight
        } else {
            pagerViewHeight.constant = newPagerViewHeight
            scrollView.contentOffset.y = 0
        }
    }
1

1 Answers

0
votes

Ok I figured it out. The scrollView was of course stopping scrolling because its content was not larger than the view itself. Once the scrollview height and header view matched the height of the entire superview - scrolling disabled. So, I added another else if condition to only set the new header view height if the scrollViewContentOffsetY was less than its absolute bottom.

        let absoluteBottom: CGFloat = scrollView.contentSize.height - scrollView.frame.size.height;
        let scrollViewOffsetY = scrollView.contentOffset.y
        let newPagerViewHeight = pagerViewHeight.constant - scrollViewOffsetY

        if newPagerViewHeight > pagerViewMaxHeight {
            pagerViewHeight.constant = pagerViewMaxHeight
        } else if newPagerViewHeight < pagerViewMinHeight && scrollViewOffsetY < absoluteBottom {
            pagerViewHeight.constant = pagerViewMinHeight

            if newPagerViewHeight < topBarHeight {
                UIView.animate(withDuration: 0.3) {
                    self.titleLabel.transform = CGAffineTransform(scaleX: 0.001, y: 0.001)
                }
                self.title = space.title
            }

            // if scrollview offset is less than its absolute bottom, adjust pagerView height
        } else if scrollViewOffsetY < absoluteBottom && Int(scrollViewOffsetY) != Int(absoluteBottom) {
            pagerViewHeight.constant = newPagerViewHeight
            scrollView.contentOffset.y = 0
        }