默认情况下如何隐藏UISearchController()?

时间:2020-12-29 20:46:50

I have UISearchController() added to my UIViewController as the following:

我将UISearchController()添加到我的UIViewController中,如下所示:

class SearchViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating  { 

let tableData = ["Here's", "to", "the", "crazy"]
var filteredTableData = [String]()
var resultSearchController = UISearchController()

@IBOutlet var tableView: UITableView!


override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.dataSource = self
    self.tableView.delegate = self
    self.resultSearchController = ({
        let controller = UISearchController(searchResultsController: nil)
        controller.searchResultsUpdater = self
        controller.dimsBackgroundDuringPresentation = false
        controller.searchBar.sizeToFit()

        self.tableView.tableHeaderView = controller.searchBar

        return controller
    })()

    // Reload the table
    self.tableView.reloadData()
}

... TableView functions ...

... TableView功能......

func updateSearchResultsForSearchController(searchController: UISearchController)
    {
        filteredTableData.removeAll(keepCapacity: false)

        let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text)
        let array = (tableData as NSArray).filteredArrayUsingPredicate(searchPredicate)
        filteredTableData = array as! [String]

        self.tableView.reloadData()
    }

I'm trying to make the search bar hidden by default, in other words, make it similar to the search bar style the "Notes" iPhone application where you have to scroll the tableView down to show the search bar. Is there a way I can achieve this? Like adding add a negative constraint that makes it hidden under the navigation bar when the ViewController loads?

我试图使搜索栏默认隐藏,换句话说,使其类似于“Notes”iPhone应用程序的搜索栏样式,您必须向下滚动tableView以显示搜索栏。有没有办法实现这个目标?就像添加一个负面约束,当ViewController加载时,它会隐藏在导航栏下面?

1 个解决方案

#1


6  

Found a very simple solution to hide the search bar by default, I found the answer here

找到一个非常简单的解决方案默认隐藏搜索栏,我在这里找到答案

I just needed to add this line in the viewDidLoad() :

我只需要在viewDidLoad()中添加这一行:

tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: false)

Updated to Swift 3.0 syntax:

更新为Swift 3.0语法:

tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: false)

#1


6  

Found a very simple solution to hide the search bar by default, I found the answer here

找到一个非常简单的解决方案默认隐藏搜索栏,我在这里找到答案

I just needed to add this line in the viewDidLoad() :

我只需要在viewDidLoad()中添加这一行:

tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: false)

Updated to Swift 3.0 syntax:

更新为Swift 3.0语法:

tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: false)