2015-11-07 91 views
3

我已经UISearchController()添加到我的UIViewController如下所示:如何使UISearchController()默认隐藏?

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功能 ...

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() 
    } 

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

+0

http://stackoverflow.com/a/31595116/4475605 – Adrian

回答

6

发现了一个非常简单的解决方案,以默认隐藏搜索栏,我找到了答案here

我只是需要加入这一行的viewDidLoad中():

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

更新斯威夫特3.0语法:

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

你可以添加该C只有在第一部分至少有一行的情况下才能发布。 – appleitung