Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Andrew Budis
Andrew Budis
2,034 Points

[Swift] How to chang navigation controller color (top bar) to Hex Color code.

Looking to change the color of the top Navigation Controller programmatically or functionally using a hex color code. I've figured out how to do this with generic colors, but have a specific color in mind.

Appreciate any pointers. Couldn't find a video that presented this abstract topic.

Thanks, Andrew

4 Answers

Andrew, it is an advanced formula to set a UIColor with hex colors so I recommend you just use the UIColor colorWithRed:Green:Blue:Alpha. Here is how you change the background color of the navigation controller:

[self.navigationController.navigationBar setBackgroundColor:[UIColor colorWithRed:255 / 255.0 green:0 / 255.0 blue:0 / 255.0 alpha:1]];

In this code we get the navigation bar from the navigation controller and set it to a red color with the UIColor colorWithRed:Green:Blue:Alpha method. When using this method, remember to divide the red, green and blue values by 255.0 because the method takes these numbers as a percentage of 1. Alpha is already out of 1 so you will not need to divide it.

If you are daring enough to try the hex decimal UIColor formula, you can find the instructions here.

If you have any questions, feel free to ask!

Andrew Budis
Andrew Budis
2,034 Points

Hi Landon,

Thanks for the response. I'm a bit confused on how to implement this. I'm getting an error when I embed the code into my ViewController.swift

Telling me "Use of unresolved identifier 'uicolorFromHex'

I must be missing something. Any other guidance you can provide? I think that at this point in my knowledge of Swift isn't vast enough to attempt anything too difficult.

Thank you! Andrew

Andrew, I have never used anything except colorWithRed:green:blue:alpha: so I am unaware of how to help with that formula. If you type in your hex code here you can get the red, green and blue values given your desired hex code and then use the colorWithRed:green:blue:alpha: method.

If you have any questions, feel free to ask!

Andrew Budis
Andrew Budis
2,034 Points

Even using the snippet you provided, I am still receiving some strange errors.

[self.navigationController.navigationBar setBackgroundColor:[UIColor colorWithRed:23 / 255.0 green:131 / 255.0 blue:251 / 255.0 alpha:1]];

Expected ',' separtator and Expected expression in container literal.

Any thoughts? Really lost with this part of Swift.

Andrew, that is Objective-C code. Here is the swift version that you will need to use in your project:

self.view.backgroundColor = UIColor(red: 100.0 / 255.0, green: 0.0 /255.0, blue: 0.0 / 255.0, alpha: 1.0)

I apologize for the inconvenience.

If you have any questions, feel free to ask!

Andrew Budis
Andrew Budis
2,034 Points

Landon,

Thanks for all your help. I'm still having problems implementing this in the AppDelegate.swift

I'm getting errors still. "AppDelegate does not have a member named view."

Any chance I am missing, or potentially inserting this within the wrong statement.

Thank you again for your help and patience.

Andrew