chuninator
chuninator

Reputation: 63

Navigation Controller Black when trying to make transparent

Completely stumped. I've looked all over and implemented every solution I could find. I can't seem to get the navigation bar to become transparent.

When trying to set the background color, I just get a black bar at the top. Same as if I try to set the background images. I've tried all of these and all of there many variations.

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

I'm using it in viewWillAppear() and in an animation when scrolling. The navigation bar is transparent, and then as you scroll navigation bar gets a white background with gray text.

  func scrollViewDidScroll(_ scrollView: UIScrollView) {
    self.navigationController?.navigationBar.barStyle = .default
    let offset =  self.tableView.contentOffset.y
    if offset > 250.0 {
        self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.darkGray]
        self.navigationController?.navigationBar.topItem?.title = spot!.Name
        self.navigationController?.navigationBar.isTranslucent = false
        self.navigationController?.navigationBar.tintColor = UIColor.darkGray
        self.navigationController?.isNavigationBarHidden = false
        self.navigationController?.navigationBar.barTintColor = UIColor.white
    }
    else {

        self.navigationController?.navigationBar.topItem?.title = nil
        self.navigationController?.navigationBar.isTranslucent = true
        self.navigationController?.isNavigationBarHidden = false
        self.navigationController?.navigationBar.tintColor = UIColor.white

        self.navigationController?.navigationBar.barTintColor = UIColor.white
        self.navigationController?.navigationBar.shadowImage = UIImage()

    }
}

Here's what it looks like...

I've also tried setting the background color to white, and changing the alpha = 0, but that doesn't work either.

Any help greatly appreciated.

Upvotes: 5

Views: 846

Answers (1)

menq
menq

Reputation: 391

This is because the window's background color is black and

You should set window?.backgroundColor = UIColor.white

in the AppDelegate application didFinishLauchingWithOptions method

Upvotes: -1

Related Questions