Let’s talk about some arts about iOS development. I’m gonna show you how to build sticky header view. Please open the GitHub project here before starting to read. You need some codes to paste into your project. End of the write, it will be like…

Image for post
Image for post

You need to create 2 classes which are StickyHeader and StickyHeaderView. These are base classes which provide us to initialize sticky header component.

We need to add an extension function to manage tableview scroll.

The classes are ready to start. The next is calling sticky header and assigning my header view to it.

Image for post
Image for post

Call your view in viewDidLoad and assign to sticky header view.

height: The maximum height.
minimumHeight: The minimum height after scrolling.

We sometimes want to change header background color dynamically. After adding header view, create navigationView which is an UIView and add to header view. So, I added one more view over MyHeaderView.

NavigationView background color will change by offset. Call scrollViewDidScroll and add this code.

That’s all. Run your project.

Here is the all codes of controller: ViewController
Here is the github project: TableStickyViewExample

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store