如何在导航栏下隐藏searchBar并在用户拉下tableView时显示它?

时间:2017-09-20 20:28:41

标签: ios swift3 uisearchbar

在我的应用程序中,我想在tableView上面实现searchBar,它将隐藏在navigationBar下。当用户拉下tableView然后我想显示searchBar。请告诉我如何在swift中实现这一目标。谢谢!! enter image description here

4 个答案:

答案 0 :(得分:1)

只需在设置搜索控制器后立即使表格视图或集合视图滚动到第一行。

override func viewDidLoad() {
  super.viewDidLoad()
  
  navigationItem.searchController = searchController
  
  tableView.scrollToRow(
    at: .init(row: 0, section: 0),
    at: [.bottom, .left],
    animated: false
  )
}

答案 1 :(得分:0)

在AppDelegate.swift中使用UINavigationController设置rootViewController。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.makeKeyAndVisible()
        window?.rootViewController = UINavigationController(rootViewController: ViewController())

        return true
    }

然后在ViewController.swift中,通过正确实现UITableViewDataSource和UITableViewDelegate方法来添加TableView。 然后,声明UISearchController并将其添加到tableHeaderView而不是添加为子视图。然后,设置tableView的内容偏移量,以便默认情况下隐藏searchController。当tableView向下滚动时,只显示searchBar。

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    private var myData: NSArray = ["One", "Two", "Three"]

    private var tableView: UITableView!

    //lazy var searchBar:UISearchBar = UISearchBar()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        navigationItem.title = "Home"

        tableView = UITableView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))

        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
        tableView.dataSource = self
        tableView.delegate = self

        self.tableView.backgroundColor = .brown

        let searchBarController: UISearchController = UISearchController(searchResultsController: nil)

        tableView.tableHeaderView = searchBarController.searchBar

        tableView.setContentOffset(CGPoint.init(x: 0, y: 44), animated: false)

        self.view.addSubview(tableView)

    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("\(indexPath.row)")
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return myData.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath)
        cell.textLabel?.text = myData[indexPath.row] as! String
        cell.backgroundColor = .brown
        return cell
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

首次启动应用程序时,隐藏了searchBar,只显示tableView: enter image description here

向下滚动tableView后,将出现searchBar:

enter image description here

答案 2 :(得分:0)

我可以使用此代码隐藏searchBar

 let searchController = UISearchController(searchResultsController: nil)

override func viewDidLoad() {
    super.viewDidLoad()

    self.searchbar()
    self.automaticallyAdjustsScrollViewInsets = false
    tableView.contentInset = UIEdgeInsets.zero
    tableView.tableHeaderView = searchController.searchBar
    //Hide SearchBar
    tableView.setContentOffset(CGPoint.init(x: 0, y: 44), animated: false)
  }

func searchbar() {
    searchController.searchResultsUpdater = self as UISearchResultsUpdating
    searchController.dimsBackgroundDuringPresentation = false
    definesPresentationContext = true

  }

答案 3 :(得分:0)

要最初隐藏searchBar,只需在数据填充了表格视图(或集合视图)之后,将navigationItem.searchController属性设置为