iosuinavigationbarlarge-titlepreferslargetitles

Navigation Bar Large Title transition delegate


I have enabled Large Title in my navigation bar with automatic Display Mode, so that it shrinks when user scrolls. Is there a way to get notified when this transition happens? I didn't found any delegate method for this. I have a Right Bar Button Item with long label which I'd like to hide when Large Title gets shrunk so that the title is perfectly centered.


Solution

  • Apparently there's no delegate nor any other official way to be notified about this. So my workaround is using ScrollViewDelegate:

    extension ViewController: UIScrollViewDelegate {
        func scrollViewDidScroll(_ scrollView: UIScrollView) {
            if let height = navigationController?.navigationBar.frame.size.height, height == 44 {
                // handle small title
            }
            else {
                // handle large title
            }
        }
    }
    

    This doesn't work on iPad as the height of navbar is different, but that's intended in my case.

    Also keep in mind that scrollViewDidScroll gets called XX times for a single small scroll, so before doing any updates, check they haven't been done already.