Set lining image of transparent navigation bar and status bar in Swift iOS 8

I am new to rapid iOS development and am facing a problem. I want to set a transparent navigation bar and make an image of the underlying transparent navigation bar and status bar, as shown below,

enter image description here

But after I executed the following code,

self.navigationController!.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default) self.navigationController!.navigationBar.shadowImage = UIImage() self.navigationController!.navigationBar.translucent = true 

As a result, the image is still below the navigation bar and the status bar, even if I set the navigation bar to transparent.

enter image description here

+5
source share
4 answers

I tried the same code as you,

 override func viewDidLoad() { super.viewDidLoad() self.navigationController!.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default) self.navigationController!.navigationBar.shadowImage = UIImage() self.navigationController!.navigationBar.translucent = true } 

And it works great, and you can see the result here:

enter image description here

Check out the project and find out what you are missing.

Hope this helps.

+7
source

if you are not using the default navigation bar, then move the image of the background image to be displayed below the 20px status bar on top, and then clear your background color of the status bar using:

  override func viewDidLoad() { super.viewDidLoad() let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView statusBar?.backgroundColor = UIColor.clear } 

if you want to change the color of the status bar elements to white, use:

  override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent } 

the conclusion will be enter image description here

+5
source

I solved this by setting a transparent UIColor for the status bar background.

  guard let statusBar = UIApplication.sharedApplication().valueForKey("statusBarWindow")?.valueForKey("statusBar") as? UIView else { return } statusBar.backgroundColor = UIColor(red: 2, green: 200.0, blue: 200, alpha: 0) // color value has no effect. Only alpha value is needed to make it transparent 
+2
source

Put this in your ViewController code.

  override func prefersStatusBarHidden() -> Bool { return true } 
-1
source

All Articles