Enes Karaosman Enes Karaosman - 2 months ago 32
iOS Question

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
}
}

Answer

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)