简体   繁体   中英

How can I mimic safari's search bar UI/animations in Swift?

I am attempting to mimic the search bar behavior below in iOS Safari on my current app: img

I mainly want the navigation bar to scroll up to a very small version when the user scrolls on the content and comes back when they scroll back up.

I've tried using scrollViewDidScroll but I cannot seem to get it to mimic that behavior. I'm unsure if I am adding the search bar correctly to the navigation bar.

let searchBar: UISearchBar = {
    let sb = UISearchBar()
    sb.autocapitalizationType = .none
    sb.autocorrectionType = .no
    sb.keyboardAppearance = UIKeyboardAppearance.default
    sb.placeholder = "Search"
    return sb
}()

fileprivate func setupNav() {
    //Basic Setup
    navigationController?.navigationBar.isTranslucent = true
    navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)

    //Search Bar
    searchBar.sizeToFit()
    navigationItem.titleView = searchBar
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    //Implement
}
let searchController                                  = UISearchController(searchResultsController: nil)
searchController.dimsBackgroundDuringPresentation     = false
searchController.searchBar.placeholder                = "Search"
searchController.searchResultsUpdater                 = self
navigationItem.searchController                       = searchController
definesPresentationContext                            = true 

Put your SearchController in the Navigation Item instead. This will add collapsable SearchBar . Additionally you can try with Navigation Item Prompt for very small title on top.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM