bircastri
bircastri

Reputation: 2169

NavigationBar and View with different background color

I have a UIView with UINavigationBar.

I'm building this code to setting background color at View and also at NavigationBar.

So this is the code:

    override func viewDidLoad(){
       super.viewDidLoad()
       self.navigationController?.navigationBar.barTintColor = getColor(red: 41, green: 151, blue: 255)
       self.view.backgroundColor = getColor(red: 41, green: 151, blue: 255)      
    }

func getColor(red: Int, green: Int, blue: Int) ->
   UIColor{
   return UIColor(red: CGFloat(Float(red) / 255.0),
      green CGFloat(Float(red) / 255.0),
      blue: CGFloat(Float(red) / 255.0),
      alpha: CGFloat(1.0))
}

As you can see the color is the same, but the output view is like this:

enter image description here

As you can see, the NavBar have the different color and I don't know why.

Upvotes: 1

Views: 813

Answers (4)

linizio
linizio

Reputation: 185

try this

self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.view.backgroundColor = UIColor.clear

instead of

self.navigationController?.navigationBar.barTintColor = getColor(red: 41, green: 151, blue: 255)

Upvotes: 2

Ahmad Farrag
Ahmad Farrag

Reputation: 234

Apparently, this is because of navigation bar isTranslucent, set it programmatically to false or if you're using storyboard it could be done by removing the check from Translucent like the photo attached.

enter image description here

Upvotes: 0

Chandler De Angelis
Chandler De Angelis

Reputation: 2776

It is probably because your navigation bar is translucent, try settings isTranslucent to false.

Upvotes: 0

user1898712
user1898712

Reputation: 368

I think you will need to set the navigation bar's isTranslucent property to false.

Upvotes: 0

Related Questions