Search code examples
swiftxcodeuiscrollviewuinavigationitem

Swift Scroll down should make Bar Item disappear


I want that the Bar Button Item diappear when I scroll down. My problem is that I don't know how to detect the scroll? I have tried some code but nothing worked. For example:

func scrollViewDidScroll(_ scrollView: UIScrollView) { print("123") } (Don't work, I call the method in viewDidLoad())

Also I don't know where I have to call the method. In viewDidLoad(), viewDidAppear() or somewhere else? I am new in Swift, sorry.

Does anyone know the answer?


Solution

  • The thing is you are calling it from viewDidLoad, that gets called only once when the view is loaded. You need to place this scrollViewDidScroll function separately, after viewDidLoad for example, but not inside of it.

    Also make sure you implemented UIScrollViewDelegate in your file. Then you can add scrollViewDidScroll method to detect when is the view scrolled and print('123') inside of it.

    Also make sure to set your scrollView.delegate = self in your viewDidLoad.