3

I have created custom header but I don't know how to animate while scrolling.

Please check below image and let me know how to animate while scrolling. This animated header in Fotmob app.

enter image description here

4

2 Answers 2

2

First of all add header view as UIvVew and add UIScrollView or UITableView below headerView same as screenshot and follow below step.

enter image description here

  1. set a fixed height constraint to the header view (125 for example) and attach it to top, left and right.

  2. make the UIScrollView below to use all the available space so set to zero top, bottom, left and right constraints.

  3.  connect the header view height constraint to the ViewController in order to have something like:

     @IBOutlet var headerViewHeightConstraint: NSLayoutConstraint!
    
  4. set the UIScrollView delegate to the ViewController

  5. declare two properties to limit the maximum and the minimum height of the header view, fox example:

    let headerViewMaxHeight: CGFloat = 125
    let headerViewMinHeight: CGFloat = 44 + UIApplication.shared.statusBarFrame.height
    
  6. The entire workaround is based on update the header view height constraint while the UIScrollView is scrolling, so let’s implement the UIScrollViewDelegate and the most important delegate for our case, the scrollViewDidScroll:

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
    
        let headerViewMinHeight: CGFloat = 44 + UIApplication.shared.statusBarFrame.height
    
        let yPos = mainScrollView.contentOffset.y
        let newHeaderViewHeight: CGFloat = headerViewHeightConstraint.constant - yPos
    
        if newHeaderViewHeight > headerViewMaxHeight {
            // Here, Manage Your Score Format View
            headerViewHeightConstraint.constant = max(headerViewMaxHeight, newHeaderViewHeight)
    
        } else if newHeaderViewHeight < headerViewMinHeight {
    
        headerViewHeightConstraint.constant = headerViewMinHeight
    
        } else {
    
            headerViewHeightConstraint.constant = newHeaderViewHeight
            scrollView.contentOffset.y = 0 // block scroll view
    
        }
    }
    
Sign up to request clarification or add additional context in comments.

Comments

0

I have created the same, Check the below image

MyFriendList

Overview

  1. TableView
  2. Sample MVVM pattern
  3. Swift 5.0 above
  4. Xcode 11 above

Find the GIT URL for code

HeaderAnimation

1 Comment

Thank you for creating a sample project, I'll check it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.