Show Search Bar in Navigation Bar Without Scrolling on iOS 11

Show search bar in navigation bar without scrolling on iOS 11

The following makes the search bar visible at first, then allows it to hide when scrolling:

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if #available(iOS 11.0, *) {
navigationItem.hidesSearchBarWhenScrolling = false
}
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if #available(iOS 11.0, *) {
navigationItem.hidesSearchBarWhenScrolling = true
}
}

Using isActive didn't do what I wanted, it makes the search bar active (showing cancel button, etc.), when all I want is for it to be visible.

Swift : show searchbar when scroll up and hide it when scroll down

All you need to do is to hide and display your search bar when you start scrolling so for that you need to override the didBeginScrolling (check the exact name ) and add this code accordingly

navigationItem.hidesSearchBarWhenScrolling = false

more info is here https://stackoverflow.com/a/46352230/5123516



Related Topics



Leave a reply



Submit