Sticky header for scrollview

Viewed 3174

I was trying to add a sticky header to my scroll view in swift. While I searched, I found an example and I implemented as follows:

override func viewDidLoad() {
    super.viewDidLoad()
    self.m_scrlvwMAinScroll.delegate = self
    self.initialOffSet = self.m_vwAnchorVw.frame.origin.y
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    var newFrame = self.m_vwAnchorVw.frame
    newFrame.origin.y = max(self.initialOffSet, scrollView.contentOffset.y)
    self.m_vwAnchorVw.frame = newFrame
}

But here the sticky view (self.m_vwAnchorVw) though stood at the top while scrolling, the other scrollview contents was scrolling over the sticky view not below the view.

I want the scrollview contents to scroll below the sticky view when scrolled up.

What change should I make in the code?

1 Answers

I believe this is the result of the order in which you added subviews to the scrollView's contentView - add the stickyView as the last view in the hierarchy (so that it is always rendered on top of the rest of the content).

Related