NavigationController configured as transparent is loaded with color from the previous view

0

I have a view configured with the NavigationController as transparent, however when the view is loaded, the initial color of the NavBar is loaded with the color of the NavBar of the view that precedes it.

In my view 1 (previous), I have my NavBar configured as follows:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    UIApplication.shared.statusBarStyle = .default
    self.navigationController?.navigationBar.barTintColor =  UIColor.red // uicolorFromHex(0x20AB7E)
    self.navigationController?.navigationBar.tintColor = UIColor.white
    self.navigationController?.navigationBar.setBackgroundImage(nil, for: UIBarMetrics.default)
    self.navigationController?.navigationBar.shadowImage = UIImage()
    self.navigationController?.navigationBar.isTranslucent = true

    let navigationBackgroundView = self.navigationController?.navigationBar.subviews.first
    navigationBackgroundView?.alpha = 1.0

    self.navigationController?.navigationBar.titleTextAttributes =  [NSForegroundColorAttributeName:UIColor.white.withAlphaComponent(1)]

}

The code may have more lines, less lines, but it works.  and in my affected view I have:

    override func scrollViewDidScroll(_ scrollView: UIScrollView) {

    if (scrollView.contentOffset.y >= 0 ) { //(bounds.height/2)
        let rest = self.tableView.contentOffset.y / (self.tableView.frame.size.height - bounds.height/2)
        UIApplication.shared.statusBarStyle = .lightContent//Default
        let navigationBackgroundView = self.navigationController?.navigationBar.subviews.first
        navigationBackgroundView?.alpha = rest //0.8
        tituloImagenEtiqueta.textColor = UIColor.white.withAlphaComponent(1-rest)
        self.navigationController?.navigationBar.setBackgroundImage(nil, for: UIBarMetrics.default)
        self.navigationController?.navigationBar.shadowImage = UIImage()
        self.navigationController?.navigationBar.tintColor = UIColor.white
        self.navigationController?.navigationBar.titleTextAttributes =  [NSForegroundColorAttributeName:UIColor.white.withAlphaComponent(rest)]
        self.navigationController?.navigationBar.barTintColor = uicolorFromHex(0x20AB7E).withAlphaComponent(rest)
        self.navigationController?.navigationBar.isTranslucent = true
        titulo.title = currentObject
    } else {
        self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
        self.navigationController?.navigationBar.shadowImage = UIImage()
        self.navigationController?.navigationBar.backgroundColor = UIColor.clear
        self.navigationController?.navigationBar.tintColor = UIColor.white
        self.navigationController?.navigationBar.barTintColor = UIColor.clear
        self.navigationController?.view.backgroundColor = UIColor.clear
        self.navigationController?.navigationBar.isTranslucent = true
        self.navigationController?.navigationBar.titleTextAttributes =  [NSForegroundColorAttributeName:UIColor.white.withAlphaComponent(0.0)]

    }


}

The above, I'm increasing the alpha of the NavBar as I move down in such a way that it becomes fully visible.

Like the previous fragment, it works. However, when the affected view loads, the color of the NavBar loads in red, and at the slightest scroll the red color disappears and now if the NavBar becomes transparent and does what is in the scrollViewDidScroll method

If in my affected view I add the following code fragment:

override func viewDidLayoutSubviews() {

    self.navigationController!.navigationBar.isTranslucent = true
    self.navigationController!.navigationBar.setBackgroundImage(UIImage(), for: .default)
    self.navigationController!.navigationBar.shadowImage! = UIImage()
    self.navigationController!.isToolbarHidden = false
    self.navigationController!.navigationBar.backgroundColor = UIColor.clear
}

When loading the affected view, it loads me, in transparent, however what is in the scrollViewDidScroll method does not work. When you scroll the NavBar's alpha does not apply so it always remains transparent.

    
asked by Víctor Gonzal 05.11.2016 в 02:04
source

0 answers