Fixed UISearchBar using UISearchController - Not using header view of UITableView

会有一股神秘感。 提交于 2019-11-30 06:32:28

问题


Is it possible to put UISearchBar of UISearchController somewhere other than header view of UITableView?

In the apple's sample code for UISearchController, following is used.

[self.searchController.searchBar sizeToFit];
self.tableView.tableHeaderView = self.searchController.searchBar; 

Is it possible to position searchBar somewhere else? Say we want to implement a fixed UISearchBar like the one used in contacts app. I've tried this but the searchBar doesn't appear at all.


回答1:


You can place the UISearchBar of UISearchController in the navigation bar so that it remains fixed

self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.searchBar.searchBarStyle = UISearchBarStyleMinimal;

// Include the search bar within the navigation bar.
self.navigationItem.titleView = self.searchController.searchBar;

self.definesPresentationContext = YES;



回答2:


Create One uiview and added searchController.searchbar inside this view. After that add this view in your viewController.view. above the table view.

then it will not scroll.

   var viewTemp = UIView(frame: CGRectMake(0.0, 64.0,320.0, 44))
   viewTemp.addSubview(self.searchController.searchBar)
   self.view.addSubview(viewTemp);



回答3:


Instead of directly keeping UISearchBar on top of UITableView. Put the UISearchBar inside a view.



来源:https://stackoverflow.com/questions/29192579/fixed-uisearchbar-using-uisearchcontroller-not-using-header-view-of-uitablevie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!