¿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.
I read [here] (https://stackoverflow.com/questions/26675608/hidesbarsonswipe-never-shows-navbar-again-when-scrolling-up) that I need to set my top constraint to the superview. that I need to set my top constraint to the superview.
But, ¿How to set my topAnchor to the superview programmatically?
¿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.

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:

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

This is about Apple devices, Apple frameworks, and Apple Technology. It is best asked over on the Apple Developer forums, StackOverFlow, or other Apple development resources. These forums are for discussions about the open-source Swift language and infrastructure

Sorry I'm stuck in this, and I'd already asked in different places.
I'm desperate.
But I can erase the question if it is not related.
Sorry!

I'm desperate.

If informal support channels haven’t panned out, you should open a DTS tech support incident and talk to one of our UIKit specialists.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

1 Like

Are you a paying Apple Developer? If so, have you asked Apple support, or filed a DTS incident report?

Yes I'm paying an apple developer account. No I don't filled yet an incident, I will do it.

Terms of Service

Privacy Policy

Cookie Policy