<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Артем Калинин</title>
    <description>The latest articles on DEV Community by Артем Калинин (@kalininartemval).</description>
    <link>https://dev.to/kalininartemval</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F995433%2F020fd348-6f34-419d-9b89-a9d5b91cb87d.JPG</url>
      <title>DEV Community: Артем Калинин</title>
      <link>https://dev.to/kalininartemval</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kalininartemval"/>
    <language>en</language>
    <item>
      <title>Creating UITableView with a dynamic header</title>
      <dc:creator>Артем Калинин</dc:creator>
      <pubDate>Fri, 23 Dec 2022 21:07:56 +0000</pubDate>
      <link>https://dev.to/kalininartemval/creating-uitableview-with-a-dynamic-header-5b1c</link>
      <guid>https://dev.to/kalininartemval/creating-uitableview-with-a-dynamic-header-5b1c</guid>
      <description>&lt;p&gt;Hello there! Recently, I had very cool experience at my work. I needed to set tableView with a dynamic header. The information in the header was complete in the initial state, and when the user was scrolling the table, some part in the header was smoothly hiding and the main part remained on the top. Cool, right?&lt;/p&gt;

&lt;p&gt;I have created two files &lt;code&gt;HeaderView&lt;/code&gt; and, of course, &lt;code&gt;ViewController&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;First of all, let’s take a look at HeaderView. I have added three views with different colors as an example in &lt;code&gt;UIStackView&lt;/code&gt; in &lt;code&gt;HeaderView&lt;/code&gt;. All the magic with a smooth hidden and alpha of this objects will be here. Also, we have var height. In the observer (&lt;code&gt;didSet&lt;/code&gt;) we will calculate an actual height of our HeaderView and make the colored views invisible or not.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private lazy var blueView: UIView = {
        let view = UIView()
        view.backgroundColor = .blue
        view.heightAnchor.constraint(equalToConstant: 72).isActive = true
        return view
    }()

    private lazy var greenView: UIView = {
        let view = UIView()
        view.backgroundColor = .green
        view.heightAnchor.constraint(equalToConstant: 72).isActive = true
        return view
    }()

    private lazy var yellowView: UIView = {
        let view = UIView()
        view.backgroundColor = .yellow
        view.heightAnchor.constraint(equalToConstant: 72).isActive = true
        return view
    }()

    private let stackView: UIStackView = {
        let stackView = UIStackView()
        stackView.axis = .vertical
        stackView.spacing = 16
        stackView.translatesAutoresizingMaskIntoConstraints = false
        return stackView
    }()

 var height: CGFloat = 340 {
        didSet {
            let diagramAlpha = 1.0 - (maxHeight - height &amp;gt; 140.0 ? 140.0 : maxHeight - height) / 140.0
            blueView.alpha = diagramAlpha
            yellowView.alpha = diagramAlpha

            if diagramAlpha &amp;lt; 0.3 {
                blueView.isHidden = true
                yellowView.isHidden = true
            } else {
                blueView.isHidden = false
                yellowView.isHidden = false
            }
            layoutIfNeeded()
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In &lt;code&gt;HeaderView&lt;/code&gt; I have min/max Height variable we needed to set actual value from view controller&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var maxHeight: CGFloat = 340
var minHeight: CGFloat = 110
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The next step – we are going to the controller. I am creating the usual &lt;code&gt;UITableView&lt;/code&gt; and adding our &lt;code&gt;HeaderView&lt;/code&gt;. For the both objects I am setting &lt;code&gt;topAnchor = view.topAnchor&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then I am creating three methods that will do all the magic.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private func calculateHeaderViewHeight(for currentOffset: CGFloat) {
        if currentOffset &amp;lt;= 0 {
            setHeaderViewHeight(for: headerView.maxHeight)
        } else {
            var newHeight = headerView.maxHeight - currentOffset
            if newHeight &amp;lt; headerView.minHeight {
                newHeight = headerView.minHeight
            }
            setHeaderViewHeight(for: newHeight)
        }
    }

    private func setHeaderViewHeight(for newHeight: CGFloat) {
        if headerViewHeightConstraint?.constant != newHeight {
            headerViewHeightConstraint?.constant = newHeight
            headerView.height = newHeight
        }
    }

    private func changeHeaderStateIfNeeded() {
        var offset = CGPoint(x: 0, y: -480)
        var tableContentInset: UIEdgeInsets = .zero
        offset = CGPoint(x: 0, y: -480)
        tableContentInset.top = 330
        tableView.contentInset = tableContentInset
        tableView.setContentOffset(offset, animated: true)
        setHeaderViewHeight(for: headerView.maxHeight)
        view.layoutIfNeeded()
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And I am adding calculateHeaderViewHeight in &lt;code&gt;func scrollViewDidScroll(_ scrollView: UIScrollView)&lt;/code&gt; that will observe &lt;code&gt;contentInset&lt;/code&gt; and &lt;code&gt;contentOffset&lt;/code&gt; and set the necessary state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let currentOffset = scrollView.contentOffset.y + scrollView.contentInset.top
        calculateHeaderViewHeight(for: currentOffset)
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And last but not least if you want to add an automatic and smooth transition for your tableView, just add this code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;COPY
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        let currentOffset = scrollView.contentOffset.y + scrollView.contentInset.top
        var offset: CGPoint = .zero

        let transition = UIViewPropertyAnimator(duration: 0.0, dampingRatio: 1) {
            if currentOffset &amp;lt; 170 {
                offset.y = -300
            } else {
                guard currentOffset &amp;lt; 276 else { return }
                offset.y = -230
            }
            DispatchQueue.main.async {
                self.tableView.setContentOffset(offset, animated: true)
            }
        }
        transition.startAnimation()
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And don’t forget the code that we need to write for the moment when we will stop dragging our tableView.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer&amp;lt;CGPoint&amp;gt;) {
        targetContentOffset.pointee.y = max(targetContentOffset.pointee.y - 1, 1)
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's see what we have finally got in &lt;code&gt;ViewController&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import UIKit

class ViewController: UIViewController {
    private var headerViewHeightConstraint: NSLayoutConstraint?

    private lazy var headerView: HeaderView = {
        let view = HeaderView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.backgroundColor = .white
        return view
    }()

    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .grouped)
        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
        tableView.showsVerticalScrollIndicator = false
        tableView.backgroundColor = .gray
        tableView.translatesAutoresizingMaskIntoConstraints = false
        return tableView
    }()

    var numbersArray = ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"]

    // MARK: - Life cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }

    // MARK: Private
    private func calculateHeaderViewHeight(for currentOffset: CGFloat) {
        if currentOffset &amp;lt;= 0 {
            setHeaderViewHeight(for: headerView.maxHeight)
        } else {
            var newHeight = headerView.maxHeight - currentOffset
            if newHeight &amp;lt; headerView.minHeight {
                newHeight = headerView.minHeight
            }
            setHeaderViewHeight(for: newHeight)
        }
    }

    private func setHeaderViewHeight(for newHeight: CGFloat) {
        if headerViewHeightConstraint?.constant != newHeight {
            headerViewHeightConstraint?.constant = newHeight
            headerView.height = newHeight
        }
    }

    private func changeHeaderStateIfNeeded() {
        var offset = CGPoint(x: 0, y: -480)
        var tableContentInset: UIEdgeInsets = .zero
        offset = CGPoint(x: 0, y: -480)
        tableContentInset.top = 330
        tableView.contentInset = tableContentInset
        tableView.setContentOffset(offset, animated: true)
        setHeaderViewHeight(for: headerView.maxHeight)
        view.layoutIfNeeded()
    }
}

// MARK: SetupUI
extension ViewController {
    private func setupUI() {
        view.addSubview(tableView)
        view.addSubview(headerView)

        view.backgroundColor = .white
        let headerHeightConstraint = headerView.heightAnchor.constraint(equalToConstant: headerView.maxHeight)
        self.headerViewHeightConstraint = headerHeightConstraint

        NSLayoutConstraint.activate([
            headerView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
            headerView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            headerView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            headerHeightConstraint,

            tableView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
            tableView.leftAnchor.constraint(equalTo: view.leftAnchor),
            tableView.rightAnchor.constraint(equalTo: view.rightAnchor),
            tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
        ])

        changeHeaderStateIfNeeded()
    }
}

// MARK: UITableViewDelegate
extension ViewController: UITableViewDataSource, UITableViewDelegate {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&amp;gt; Int {
        return numbersArray.count
    }

    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -&amp;gt; UIView? {
        return nil
    }

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -&amp;gt; CGFloat {
        return .leastNormalMagnitude
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&amp;gt; UITableViewCell {
        let cell = UITableViewCell()
        var contentConfiguration = UIListContentConfiguration.sidebarCell()
        contentConfiguration.text = numbersArray[indexPath.row]
        cell.contentConfiguration = contentConfiguration
        cell.backgroundColor = .gray
        return cell
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -&amp;gt; CGFloat {
        return 64
    }
}

// MARK: UIScrollView
extension ViewController {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let currentOffset = scrollView.contentOffset.y + scrollView.contentInset.top
        calculateHeaderViewHeight(for: currentOffset)
    }

    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        let currentOffset = scrollView.contentOffset.y + scrollView.contentInset.top
        var offset: CGPoint = .zero

        let transition = UIViewPropertyAnimator(duration: 0.0, dampingRatio: 1) {
            if currentOffset &amp;lt; 170 {
                offset.y = -300
            } else {
                guard currentOffset &amp;lt; 276 else { return }
                offset.y = -230
            }
            DispatchQueue.main.async {
                self.tableView.setContentOffset(offset, animated: true)
            }
        }
        transition.startAnimation()
    }

    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer&amp;lt;CGPoint&amp;gt;) {
        targetContentOffset.pointee.y = max(targetContentOffset.pointee.y - 1, 1)
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkl66e7v12eppt2sjyus1.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkl66e7v12eppt2sjyus1.gif" alt="Image description" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope you have enjoyed this article and it has been useful for you. Thanx for reading! And Merry Christmas and Happy New Year!&lt;/p&gt;

</description>
      <category>watercooler</category>
    </item>
  </channel>
</rss>
