I’ve recently been working on a personal project and the past week has mostly been focused on design. I finally decided on a color scheme, downloaded some cool fonts, and made the app look a lot cleaner than the mess I had before. During this process, I noticed that the back button on my navigation bar didn’t look great — the standard Apple blue just didn’t mesh well with my design scheme. I knew it was possible to change that button but what was supposed to be a simple design fix turned into a harrowing journey. Let me just state: the way to do this is actually very simple, I just overcomplicated it. So I’m going to walk through how to do it and offer some tips along the way so you can avoid unnecessarily raising your blood pressure.
Selecting Your Image
What I thought was going to be the easiest part turned out to be the thing that tripped me up the most. I had found a button online that I liked but the color just wasn’t right. So I put it into Figma, changed the color, and exported it into Xcode. As it turns out, the sizing was all off so every time I wrote the code to set that image as the back button, it was huge, the standard Apple blue, and pretty much off the screen. I spent hours searching Stack Overflow and Google, thinking that it was the code that was wrong (there are a lot of solutions out there!). As I came to find out much later, the problem was that my image was the wrong size!
So my recommendation is to go to a website like icons8 or flaticon and find an image that you like. On flaticon, you can even change the color on a lot of them to a custom color (which is what I ended up doing) and then just download it as a png. In my case, I went with this simple back arrow and changed the color to the brown I am using throughout my app:

Setting the Back Button
Now that you have the correct image and it is the correct size, you can drag and drop it into the Assets folder of your project and set the name to something simple, like “back.”
In my case, I have several navigation controllers across several view controllers and I want them all to have the same custom back button. I thought it would be a pain to do the custom back button in each separate file but as luck would have it, you can do a lot of customization (navigation bars, navigation items, segmented controls, etc) for your app in the AppDelegate!
First, I set a constant for the appearance of the bar button item:

All this does is make it easy to customize and change the appearance of UIBarButtonItems. It also saves you from having to type out “UIBarButtonItem.appearance()” multiple times if you have several things you want to customize.
Next, in the didFinishLaunchingWithOptions method, I declared a constant for the button image and set that image equal to the back button:
So first, I declared the constant and set it equal to my image. I then modified the image a little bit so that it would look right as the back button. You might need to mess around with this a little bit but I found that those numbers worked pretty well for me. Finally, I used the setBackButtonBackgroundImage to the constant I made earlier. And voila! I have a custom back button throughout my app:

Custom Text
As you can see, I also did away with any text that would normally appear like “back” or the name of the previous view controller. I tried to find a way to do that in the AppDelegate as well but nothing I did worked. Instead, I implemented the code below in each of my segues:

In my research on this, I found an interesting tidbit — the navigation item technically belongs to the parent view controller (the one that the segue comes from). So in order to set the title for the back button, you need to do so in the prepareForSegue method of the parent view controller. The code is pretty simple: declare a constant for a bar button, set the title as a blank string, and set that constant equal to the backBarButtonItem on the navigation item. It can be a little tedious to do this for each segue in your application, but copy and paste is a heck of a helper and in the end, it is worth it to have your app looking the way you want it.
I hope you found this helpful and if anyone has any other suggestions or ways to do this, I’d love to know!
origin: https://medium.com/
Comments
Post a Comment