Search code examples
iosuiscrollviewswift4navigationbaruicolor

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

  • 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)
        }
    }