How to hide the navigation bar as scroll down, when constraints are programmatically?

I'm trying to hide the navigation bar when the scroll is down, and unhide again when the scroll is up. But when I scroll up the navigation bar doesn't appear again.
How can I fix that behavior?

The first detail is that I'm not using story board.

The second one is that I'm separating all my UI elements from my ViewController, so I have a UIView class where I'm declaring all my UI elements, and in my ViewController I only call my UIView. Here is my code.

Code Block
class RegisterUIView: UIView {
lazy var scrollView: UIScrollView = {
let scroll: UIScrollView = UIScrollView()
scroll.contentSize = CGSize(width: self.frame.size.width, height: self.frame.size.height)
scroll.translatesAutoresizingMaskIntoConstraints = false
return scroll
}()
/*Here other UI elements*/
override init(frame: CGRect) {
super.init(frame: frame)
initComponents()
}
required init?(coder: NSCoder) {
      super.init(coder: coder) 
    }
func initComponents() {
addComponents()
setupLayout()
}
func addComponents() {
addSubview(scrollView)
}
func setupLayout() {
NSLayoutConstraint.activate([
scrollView.topAnchor.constraint(equalTo: self.topAnchor),
scrollView.leadingAnchor.constraint(equalTo: self.leadingAnchor),
scrollView.bottomAnchor.constraint(equalTo: self.bottomAnchor),
scrollView.trailingAnchor.constraint(equalTo: self.trailingAnchor)
])
}
}

My ViewController

Code Block
class RegisterViewController: UIViewController {
private let registerView: RegisterUIView = {
let view = RegisterUIView(frame: .zero)
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.hidesBarsOnSwipe = true
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.hidesBarsOnSwipe = false
}
override func viewDidLoad() {
super.viewDidLoad()
setupLayout()
}
func setupLayout() {
view.addSubview(registerView)
NSLayoutConstraint.activate([
registerView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),
registerView.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor),
registerView.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor),
registerView.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor)
])
}



How to hide the navigation bar as scroll down, when constraints are programmatically?
 
 
Q