4

My apps is currently contains the 2 UIViewController VC1 to VC2.

In VC1 is the home screen and has an orange gradient image on UINavigationBar

In VC2 has a translucent UINavigationBar and set UIImage on UINavigationBar

but when navigation between this two view controllers, it appears a black navigation bar until the transition have done. I have try to set setNavigationBarHidden but I do not want hide the navigation bar, so how can I remove the black navigation bar?

Like this-

navigation from VC1 to VC2

In VC1

override func viewWillAppear(animated: Bool) {        
    if let navController = self.navigationController {

        UIGraphicsBeginImageContext(gradientLayer.frame.size)
        gradientLayer.renderInContext(UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        UIView.animateWithDuration(0.3, animations: {
            navController.navigationBar.setBackgroundImage(image, forBarMetrics: .Default)
            navController.navigationBar.translucent = false
        })
    }
}

in VC2

override func viewWillAppear(animated: Bool) { 
    UIView.animateWithDuration(0.3, animations: {
            self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
            self.navigationController?.navigationBar.shadowImage = UIImage()
            self.navigationController?.navigationBar.translucent = true
        })
}


override func viewWillDisappear(animated: Bool) {        
    if let navController = self.navigationController {

        UIGraphicsBeginImageContext(gradientLayer.frame.size)
        gradientLayer.renderInContext(UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        UIView.animateWithDuration(0.3, animations: {
            navController.navigationBar.setBackgroundImage(image, forBarMetrics: .Default)
            navController.navigationBar.translucent = false
        })
    }
}
cs.1007
  • 53
  • 1
  • 9
  • check this post http://stackoverflow.com/questions/39835420/navigationbar-delay-updating-bartintcolor-ios10/40255483#40255483 – Joe Mar 23 '17 at 22:07

5 Answers5

9

If you want to hide navigation bar without black color in transition. (It's in second screen)

Swift4:

override func viewWillAppear(_ animated: Bool) {
  self.navigationController?.setNavigationBarHidden(true, animated: true)  
  }
user9138459
  • 91
  • 1
  • 2
2

Swift 5 Simple way

//MARK:- Only use this code where you want to hide the navigation bar 
self.navigationController?.setNavigationBarHidden(true, animated: true)
Shakeel Ahmed
  • 5,361
  • 1
  • 43
  • 34
0

You will need to set the NavigationBar color directly with e.g.: navigationController.navigationBar.barTintColor = .orange

Dheeraj D
  • 4,386
  • 4
  • 20
  • 34
Yohst
  • 1,671
  • 18
  • 37
0

It depends on how you are setting the color of your Navigation Bar. If you set it in the next view's viewDidLoad then when you press a button for the transition it will change before the view pushes the next controller.

What timing do you want it to change at?

For Before: viewDidLoad or viewWillAppear

For After: viewDidAppear

And make sure you aren't setting it or the imageView on viewWillDisappear so it won't be black.

monolith
  • 90
  • 1
  • 9
  • I put in this way: In VC1, viewWillAppear set orange gradient nav bar. If move to VC2, viewWillAppear(VC2) set image to nav bar and when press back button, viewWillDisappear set the nav bar to orange gradient. – cs.1007 Jan 05 '17 at 06:15
0

Obj C:

self.extendedLayoutIncludesOpaqueBars = true;

Swift:

extendedLayoutIncludesOpaqueBars = true
Shobhit C
  • 828
  • 10
  • 15