Custom Navigation Bar
Did you know that you can customize a Navigation Bar? Let’s do it. It’s very easy.
Create a new project. Go to storyboard. Click on the ViewController. Go to “Editor -> Embed in -> Navigation Controller”. So, a Navigation Bar was created. Now add two bar buttons.
Add an image to your Assets.xcassets folder that you would like to display in the Navigation Bar.
Let’s code now. Go to ViewController.swift.
Write a new function override func viewDidAppear(animated: Bool).
Then add this code to the function:
Now let’s go through code line by line.
- Initializing the Navigation Bar.
- Defining the style of our Navigation Bar.
- Setting the color of our bar buttons.
- Initializing the imageView, so we have a place to display our image.
- Creating an image and setting it to our imageView.
- Adding imageView to our Navigation Bar.
- Done. Run the project
Your Navigation Bar was customized. Super easy.