当 tableView 向下滑动时显示 UISearchController [英] Show UISearchController when tableView swipe down

查看:47
本文介绍了当 tableView 向下滑动时显示 UISearchController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过 UISearchController 在我的测试应用程序中实现了搜索栏.当我启动我的应用程序时,我看到导航控制器下的搜索栏.但是如何在应用程序启动时隐藏它并仅在我拉下表格视图时才显示它?并在拉出表格视图时再次隐藏?我在 google 或 youtube 上找不到任何解决方案,请帮忙.

I implemented the search bar in my test application via UISearchController. when I start my app, I see the search bar under the navigation controller. but how i can hide it when app is starts and show it only when I pull a table view down? and hide again when pull up a table view? I can't find any solution in google or youtube, please help.

这就是我在 viewDidLoad

//set the searchController
searchController = UISearchController(searchResultsController: nil)
searchController.searchBar.sizeToFit()
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search your restaurant"
tableView.tableHeaderView = searchController.searchBar
definesPresentationContext = true

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

推荐答案

将以下部分放在您的 viewDidLoad 中,所以发生的事情是您要求 UITableView 滚动并隐藏开头的 SearchBar :

Place the following segment in your viewDidLoad, so whats happening is your asking the UITableView to scroll and hide the SearchBar at the beginning :

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

并且不要忘记将 TableViewHeader 设置为您的 searchController :

And don't forget to set the TableViewHeader to your searchController :

tableView.tableHeaderView = searchController.searchBar

更新:

因为你有一个空的tableview,所以替换以下内容:

Because you have an empty tableview then replace the following :

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

与:

self.tableView.contentOffset = CGPointMake(0.0, 44.0)

祝你好运!

这篇关于当 tableView 向下滑动时显示 UISearchController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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