简体   繁体   中英

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:

在此处输入图片说明

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

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)

我认为您需要将导航栏的isTranslucent属性设置为false

可能是因为你的导航栏是半透明的,试试把isTranslucent设置为false。

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.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM