Freshley Kremp
Freshley Kremp

Reputation: 77

Swift - Issue in making the navigation bar transparent in iOS

I have a problem in making the NavigationBar transparent instead it shows the white as background color.

I wish to achieve this

enter image description here

But instead i am getting the following NavigationBar as background color.

It is showing white as background color. It is not getting transparent

enter image description here

Upvotes: 0

Views: 366

Answers (3)

RGB_DS
RGB_DS

Reputation: 118

self.navigationController?.navigationBar.setBackgroundImage(imageWithColor(color: color.withAlphaComponent(alpha)), for: .default)
self.navigationController?.navigationBar.shadowImage = imageWithColor(color: color.withAlphaComponent(alpha))
fileprivate func imageWithColor(color : UIColor) -> UIImage? {
    let rect = CGRect(x: 0.0, y: 0.0, width: 1.0, height: 1.0)
    UIGraphicsBeginImageContext(rect.size)
    let ctx = UIGraphicsGetCurrentContext()
    ctx?.setFillColor(color.cgColor)
    ctx?.fill(rect)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image
}

call in func viewWillAppera . you will get

Upvotes: 0

Harshal Shah
Harshal Shah

Reputation: 427

You also try below code.

  self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default);
    self.navigationController?.navigationBar.shadowImage = UIImage()
    self.navigationController?.navigationBar.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)
    self.navigationController?.navigationBar.isTranslucent = true

Upvotes: 0

ami rt
ami rt

Reputation: 933

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

Upvotes: 1

Related Questions