Search code examples
iosswiftuinavigationcontrolleruinavigationbar

large Navigation Bar backGround gets clear color when swiping back to root viewController


I have used largeNavigationBar and it's ok until i swipe back to root view controller and large navigation gets clear color in a nasty way. Here's the code:

func largeNavigationTitle() {
   
    self.navigationController?.view.backgroundColor = VVUtility.navigationBarColor()
    let productTitle = request?.product?.name
    self.navigationItem.title = "\(productTitle ?? " ")".localized()
    self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white, NSAttributedStringKey.font : VVUtility.normalFontWithPlusSize(increaseSize: -2.0)]
    
    if #available(iOS 11.0, *) {
        self.navigationController?.navigationBar.prefersLargeTitles = true
        self.navigationController?.navigationBar.backgroundColor = VVUtility.splashBackGroundColor()
        self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white, NSAttributedStringKey.font : VVUtility.normalFontWithPlusSize(increaseSize: 0.0)]
    } else {
        // Fallback on earlier versions
    }
    
}

I've recalled largeNavigationTitle() in both viewWillAppear and viewDidLoad

UPDATE

Here is screenshot of two states:

Before swiping: imgur.com/a/ZcSOrov

When swiping: imgur.com/a/DYeeot8


Solution

  • Try this. It should set your root View controller's navigationBar's colour to the one you wanted:

    func largeNavigationTitle() {
    
        self.navigationController?.view.backgroundColor = VVUtility.navigationBarColor()
       //add the two lines below
        self.navigationController?.navigationBar.barTintColor = VVUtility.navigationBarColor()
        self.navigationController?.navigationBar.isTranslucent = false
    
        let productTitle = request?.product?.name
        self.navigationItem.title = "\(productTitle ?? " ")".localized()
        self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white, NSAttributedStringKey.font : VVUtility.normalFontWithPlusSize(increaseSize: -2.0)]
    
        if #available(iOS 11.0, *) {
            self.navigationController?.navigationBar.prefersLargeTitles = true
            self.navigationController?.navigationBar.backgroundColor = VVUtility.splashBackGroundColor()
            self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white, NSAttributedStringKey.font : VVUtility.normalFontWithPlusSize(increaseSize: 0.0)]
        } else {
            // Fallback on earlier versions
        }
    
    }