iosswiftuinavigationbarpreferslargetitles

How to create a left aligned title with prefersLargeTitles in navigationBar?


I'm using prefersLargeTitles on my navigation bar, When it collapse, I want to align the title to the left of the navigation bar,

What I did is I create a leftBarButtonItem with a custom view of label and set the title color to .clear, the problem is, that label is visible even if the large titles didn't collapsed yet.

This is my code for adding navigation item as my title

let label = UILabel()
label.textColor = UIColor.black
label.text = "my left title"
self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(customView: label)

or the other question for this post is, how can I hide the leftBarButtonItem if the large title didn't collapsed yet?


Solution

  • Here's a link which can help you to find a way out as you want : Change title according to navigation bar.

    Some basic things would like to add is you can set navigation bar title to empty string or something else as you want and make your custom view visible if the bar is collapsed other then that make it invisible. You can check in the link how to observe if navigation bar is collapsed or not.

    One more important thing is you should remove observers if you are switching to another screen or it can cause memory leaks.