1

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?

Dylan
  • 1,121
  • 1
  • 13
  • 28

1 Answers1

1

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.

WhiteSpidy.
  • 1,107
  • 1
  • 6
  • 28
  • so, should I set the value of the oberver on viewDidAppear and remove it on viewWillDisappear? – Dylan Sep 04 '20 at 08:48
  • You can do that in multiple ways, if you are doing in a presented a View Controller then you should remove it while dismissing your View Controller or else you can remove at viewDidDisappear which means another View Controller is pushed/presented and you can remove observers of this View Controller freely or you can remove it in viewWillDisappear i.e before pushing/presenting another View Controller and you can initialize your observers in the same way. – WhiteSpidy. Sep 04 '20 at 09:28