向下滚动后再次隐藏 SearchController [英] Hide SearchController again after scrolling down

查看:24
本文介绍了向下滚动后再次隐藏 SearchController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我让 SearchController 与我的 TableView 一起显示,这是启动时与向上滚动时的样子:

这是完美的,因为我希望它在不使用时隐藏.但是,显示后,我无法通过向下滚动再次隐藏它.我如何获得该功能?

导入 UIKit类 ViewController:UIViewController、UITableViewDataSource、UITableViewDelegate、UISearchControllerDelegate、UISearchResultsUpdating {@IBOutlet 弱变量 tableView:UITableView!func updateSearchResults(for searchController: UISearchController) {让 searchBar = searchController.searchBarfilterContentForSearchText(searchBar.text!)}var isSearchBarEmpty: Bool {返回 searchController.searchBar.text?.isEmpty ??真的}func filterContentForSearchText(_ searchText: String) {过滤字符串 = stockArr.filter {(字符串:字符串)->布尔输入返回 string.lowercased().contains(searchText.lowercased())}tableView.reloadData()}varfilteredStrings: [String] = []var searchController : UISearchController!覆盖 func viewDidLoad() {super.viewDidLoad()tableView.dataSource = selftableView.delegate = self让 searchController = UISearchController(searchResultsController: nil)searchController.searchResultsUpdater = selfsearchController.obscuresBackgroundDuringPresentation = falsesearchController.searchBar.placeholder = "搜索糖果";navigationItem.searchController = searchControllernavigationItem.hidesSearchBarWhenScrolling = false定义PresentationContext = true}}

我玩弄添加

tableView.tableHeaderView = searchController.searchBarnavigationController?.navigationBar.prefersLargeTitles = truenavigationItem.title = "你好";

但它看起来不像那里的普通 SearchController(我不明白为什么我不能让它像其他人似乎都在工作一样工作,它应该根据我在这里看到的内容自动具有此功能

解决方案

您可能需要检查 UITableView 的滚动方向并通过手动禁用 UISearchController代码.方法如下:

var lastContentOffset: CGFloat = 0//声明前一个偏移量以供参考覆盖 func viewDidLoad() {super.viewDidLoad()//...searchController = UISearchController(searchResultsController: nil)//移除 letnavigationItem.hidesSearchBarWhenScrolling = true//...}func scrollViewDidScroll(_ scrollView: UIScrollView) {navigationItem.searchController = lastContentOffset <;scrollView.contentOffset.y ?零:搜索控制器}

I got SearchController to show up with my TableView, this is what is looks like on launch vs when I scroll up:

This is perfect since I want it hidden when not in use. However, after showing it, I can't hide it again by scrolling down. How do I get that functionality?

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchControllerDelegate, UISearchResultsUpdating {
    
    @IBOutlet weak var tableView: UITableView!        
    
    func updateSearchResults(for searchController: UISearchController) {
        let searchBar = searchController.searchBar
        filterContentForSearchText(searchBar.text!)
    }

    var isSearchBarEmpty: Bool {
      return searchController.searchBar.text?.isEmpty ?? true
    }

    func filterContentForSearchText(_ searchText: String) {
      filteredStrings = stockArr.filter { (string: String) -> Bool in
        return string.lowercased().contains(searchText.lowercased())
      }
      tableView.reloadData()
    }
    var filteredStrings: [String] = []

    var searchController : UISearchController!

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.dataSource = self
        tableView.delegate = self
        

        let searchController = UISearchController(searchResultsController: nil)

        searchController.searchResultsUpdater = self

        searchController.obscuresBackgroundDuringPresentation = false

        searchController.searchBar.placeholder = "Search Candies"

        navigationItem.searchController = searchController
        navigationItem.hidesSearchBarWhenScrolling = false
        
        definesPresentationContext = true
  
    }
}

Edit:

I played around with adding

tableView.tableHeaderView = searchController.searchBar
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.title = "hello"

but it doesn't look like the normal SearchControllers out there (I don't get why I can't make it work like everyone else's seems to be working, it should automatically have this functionality based on what I saw on here http://blog.eppz.eu/swiftui-search-bar-in-the-navigation-bar/)

解决方案

You'd probably have to check for the scroll direction of UITableView and disable the UISearchController manually via code. Here's how:

var lastContentOffset: CGFloat = 0 // declare the previous offset for reference

override func viewDidLoad() {
    super.viewDidLoad()
    //...
    searchController = UISearchController(searchResultsController: nil) // remove let
    navigationItem.hidesSearchBarWhenScrolling = true
    //...
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    navigationItem.searchController = lastContentOffset < scrollView.contentOffset.y ? nil : searchController
}

这篇关于向下滚动后再次隐藏 SearchController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆