简体   繁体   中英

How to fix transparency of navigation bar in swift?

I have transparent navigation bar with a background image for view controller, But when I add a bar button item to navigation bar, it becomes like in the second picture. How do I have bar button items also fully transparent navigation bar.

I used these code below to make the navigation bar transparent;

  extension UINavigationController {

  public func presentTransparentNavigationBar() {
    navigationBar.setBackgroundImage(UIImage(), forBarMetrics:UIBarMetrics.Default)
    navigationBar.translucent = true
    navigationBar.shadowImage = UIImage()
    setNavigationBarHidden(false, animated:true)
  }

  public func hideTransparentNavigationBar() {
    setNavigationBarHidden(true, animated:false)
    navigationBar.setBackgroundImage(UINavigationBar.appearance().backgroundImageForBarMetrics(UIBarMetrics.Default), forBarMetrics:UIBarMetrics.Default)
    navigationBar.translucent = UINavigationBar.appearance().translucent
    navigationBar.shadowImage = UINavigationBar.appearance().shadowImage
  }
}

This should create a transparent UINavigationBar with items in it. It's currently working fine for me.

    let navigationBarAppearace = UINavigationBar.appearance()
    navigationBarAppearace.tintColor           = UIColor.whiteColor()
    navigationBarAppearace.translucent         = true
    navigationBarAppearace.shadowImage         = UIImage()
    navigationBarAppearace.backgroundColor     = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)
    navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]
    navigationBarAppearace.setBackgroundImage(UIImage(), forBarMetrics: .Default)

Try:

if let navBar = self.navigationController?.navigationBar {
    extendedLayoutIncludesOpaqueBars = true    
    navigationBar.translucent = true
    navigationBar.backgroundColor = UIColor.clearColor()
    navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
    navigationBar.shadowImage = UIImage()

}

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