Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - how to remove black navigation bar when navigate between 2 view controller

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
        })
    }
}
like image 595
cs.1007 Avatar asked Feb 06 '23 09:02

cs.1007


1 Answers

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)  
  }
like image 78
user9138459 Avatar answered Feb 13 '23 06:02

user9138459