Reputation: 680
I wanna change the background color of my navbar, while set the translucency to false, It's just plain white and it won't change untill I set an image for the background color. any help would be appreciated.
let homeViewController = HomeViewController();
let navigationController = UINavigationController(rootViewController: homeViewController);
let navBar = navigationController.navigationBar;
navBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white];
navBar.setBackgroundImage(UIImage(), for: .default);
navBar.shadowImage = UIImage();
navBar.isTranslucent = false;
navBar.backgroundColor = UIColor.red; //not working
navBar.tintColor = UIColor.red //not working
* EDIT *
Thank you so much you guys, yea that was it, can you please help me a bit further. the reason I wanted to change the color of navbar was because I wanted to make it transparent so I could add a gradient background to it, and with barTintColor now I could set it to clear. but still this
navBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white];
navBar.setBackgroundImage(UIImage(), for: .default);
navBar.shadowImage = UIImage();
navBar.isTranslucent = false;
navBar.barTintColor = .clear;
doesn't work and I still don't see my gradient which I set like this :
let bg = UIImageView(image: UIImage(named: "gradient"));
bg.frame = CGRect(x: 0, y:0, width: UIApplication.shared.statusBarFrame.width, height: UIApplication.shared.statusBarFrame.height + (navBar.frame.height));
bg.contentMode = .scaleAspectFill;
bg.layer.masksToBounds = true;
bg.layer.opacity = 1;
navigationController.view.insertSubview(bg, belowSubview: navBar);
The reason I'm not just setting the background image with setBackgroundImage
is I wanna change the position of the background image in the navbar so it should be on the right of the navbar.
I know I got the answer to my question but this was the real one and still can't figure it out. I'm gonna mark the best answer anyway either if you guys answer me or not but I would really appreciate if you could help me further.
Added Image
Upvotes: 1
Views: 1492
Reputation: 2741
This property is for Bar Text Color.
navBar.tintColor
Use This: (for NavBar Background Color).
navBar.barTintColor
Upvotes: 2
Reputation: 2513
You set the wrong property.
Set barTintColor
to apply your background color:
navBar.barTintColor = UIColor.red
Here's the Apple docs: barTintColor
Upvotes: 3