'navigation bar back ground color will change into wrong color when table view scrolls in swift 4

I seen this video on you tube https://www.youtube.com/watch?v=rNy6aQQYbuY But the problem is that navigation bar color will not change color into correct color that I want to be

so here is the codes

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController?.navigationBar.isTranslucent = true
    self.navigationController?.navigationBar.tintColor = .white
    if #available(iOS 11.0, *) {
        self.profileTV.contentInsetAdjustmentBehavior = .never
    } else {
        automaticallyAdjustsScrollViewInsets = false
    }
    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)

    self.navigationController?.navigationBar.shadowImage = UIImage()
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
     print(scrollView.contentOffset.y)
    var offset = scrollView.contentOffset.y / 150
    if offset > 1 {
        offset = 1
        let color = UIColor(red: 181, green: 40, blue: 56, alpha: offset)
        self.navigationController?.navigationBar.backgroundColor = color
        UIApplication.shared.statusBarView?.backgroundColor = color
    } else {
        let color = UIColor(red: 181, green: 40, blue: 56, alpha: offset)
        self.navigationController?.navigationBar.backgroundColor = color
        UIApplication.shared.statusBarView?.backgroundColor = color
    }
}


extension UIApplication {
  var statusBarView: UIView? {
      return value(forKey: "statusBar") as? UIView
  }
}

the color after scrolling will be white But I want to be the color code that I wrote in my codes



Solution 1:[1]

change your code to:

func setNavigation() {
    if #available(iOS 11.0, *) {
        self.tV.contentInsetAdjustmentBehavior = .never
    } else {
        automaticallyAdjustsScrollViewInsets = false
    }
    self.navigationController?.navigationBar.tintColor = .red
    self.navigationController?.navigationBar.isTranslucent = true


    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)

    self.navigationController?.navigationBar.shadowImage = UIImage()

}


func scrollViewDidScroll(_ scrollView: UIScrollView) {

    var offset = scrollView.contentOffset.y / 1500


    if offset >= 1 {
        offset = 1
        self.navigationController?.navigationBar.backgroundColor = UIColor.white.withAlphaComponent(offset)
      //  self.navigationController?.navigationBar.alpha = offset
      //  print(offset - 0.399)
        UIApplication.shared.statusBarView?.backgroundColor = UIColor.red.withAlphaComponent(offset)
    } else {
        self.navigationController?.navigationBar.backgroundColor = UIColor.white.withAlphaComponent(offset)
        UIApplication.shared.statusBarView?.backgroundColor = UIColor.red.withAlphaComponent(offset)
    }
}

Solution 2:[2]

Put this extension wherever you want :

public extension UIImage {
    convenience init(withBackground color: UIColor) {

        let rect: CGRect = CGRect(x: 0, y: 0, width: 1, height: 1)
        UIGraphicsBeginImageContext(rect.size);
        let context:CGContext = UIGraphicsGetCurrentContext()!;
        context.setFillColor(color.cgColor);
        context.fill(rect)

        let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()

        self.init(ciImage: CIImage(image: image)!)
    }
}

It makes a UIImage using a UIColor. change the color alpha and set it as your navigationBar's backGroundImage. Here is a sample of how to use it:

  func scrollViewDidScroll(_ scrollView: UIScrollView) {
    DispatchQueue.main.async {
        var offset = scrollView.contentOffset.y
            self.navigationController?.navigationBar.setBackgroundImage(UIImage(withBackground: UIColor.init(red: 0, green: 0, blue: 0, alpha: offset * 0.1)), for: .default)
    }
}

enter image description here

Solution 3:[3]

let appearance = UINavigationBarAppearance()
appearance.backgroundColor = UIColor(named: "BackgroundColor")
appearance.titleTextAttributes = [.foregroundColor: UIColor(named: "TextColor") ?? .white]
      
self.navigationController?.navigationBar.scrollEdgeAppearance = appearance
self.navigationController?.navigationBar.standardAppearance = appearance

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mohammad_Z74
Solution 2 MohyG
Solution 3