Learn about that back bar title, and what you can and can't do with it.
- [Instructor] The navigation bar is one of those things that seems simple but can be very tricky. The title and back button has given more than a few developers a bit of a headache figuring out how to use them. Let's look at how to use them correctly. I made a simple demo of a navigation controller, a root controller, and a child view controller. If you run the application you can press the button and head over to the watercolor of the lady. At the top you'll find the back button, which takes you back to the view controller.
There's no titles here yet. Stop the app and let's set some titles. Head to the view controller. I did a little set up here. And in the view will appear in the prepare methods to the view controller. I'll start however in the view did load with the view controller's title property. And all I need to do here is type title equals gallery. Now go ahead and run this, I'm doing it again on an iPhone 8+.
And you'll get the title and navigation bar. Now go ahead and press the lady button and you'll see something interesting. The back button reflects the title of the previous controller. This gives you the ability to customize it if you're a bit sneaky. Go ahead and stop the app. I'm gonna go down to prepare for segue and in here I'm gonna change the title for this controller to return to gallery.
And then I'm gonna go up to the view will appear and change it back to gallery. So what I end up doing is every time I leave this view controller I'll put return to gallery as my title, making my back button return to gallery and when I come back I'll change it back to gallery. Now I'm also gonna add a title to the image and so I'm gonna add this to prepare.
If segue.identifier equals lady let vc equal segue.destination as lady view controller. And then I'm gonna change the title on lady view controller to lady. Okay. Go ahead and run that.
And we got gallery up here now, hit lady. You'll see we have return to gallery here, we have our title lady here. I can hit the return to gallery and I'm back to gallery again. Now there's one more thing about the back button I want to show you. It can edit itself depending on content conflict with the titles. So change the title content for vc to this. Lady watercolor, January 2010.
Now run again, click lady. And notice what happened with the back button. It's now back, it's no longer staying the full title we had back, return to gallery. Now if you add even more text it'll shrink even more. So stop the app, and I'm gonna change this to include the artist's name. Run again, and you can see it's dropped all the way down to just the chevron itself.
Some developers like that chevron, like this without a big title. There is one more way to get that instead of pushing a title like this. So let's change the title of this slide back to lady watercolor. So I'm gonna go down here to vc title again. I'm gonna delete all that, and then where I have title here in prepare for segue for return to gallery I'm gonna remove that with just a comment and I'm gonna change this to title equals quote quote.
If you have an empty string it will just print the empty string. And you can go ahead and do that, click lady, and now you have just the empty chevron here. It takes a little backward thinking to customize the back button. It reflects the view controller below it on the stack. As I've shown here, with a few tricks you can customize that button on one controller by customizing the controller preceding it.