TableSearchViewController.m 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // TableSearchViewController.m
  3. // CommonLibrary
  4. //
  5. // Created by AlexiChen on 16/2/18.
  6. // Copyright © 2016年 AlexiChen. All rights reserved.
  7. //
  8. #import "TableSearchViewController.h"
  9. @implementation TableSearchResultViewController
  10. - (void)addHeaderView
  11. {
  12. }
  13. - (void)addFooterView
  14. {
  15. }
  16. - (void)updateSearchResultsForSearchController:(UISearchController *)searchController
  17. {
  18. }
  19. @end
  20. @interface TableSearchViewController () <UISearchControllerDelegate>
  21. @end
  22. @implementation TableSearchViewController
  23. - (Class)searchResultControllerClass
  24. {
  25. return [TableSearchResultViewController class];
  26. }
  27. - (void)addSearchController
  28. {
  29. _searchResultViewController = [[[self searchResultControllerClass] alloc] init];
  30. _searchController = [[UISearchController alloc] initWithSearchResultsController:_searchResultViewController];
  31. _searchController.delegate = self;
  32. _searchController.searchResultsUpdater = _searchResultViewController;
  33. // 必须要让searchBar自适应才会显示
  34. [_searchController.searchBar sizeToFit];
  35. _searchController.searchBar.delegate = _searchResultViewController;
  36. [_searchController.searchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone];
  37. _searchController.searchBar.backgroundImage = [UIImage imageWithColor:kBackGroundColor];
  38. //把searchBar 作为 tableView的头视图
  39. self.tableView.tableHeaderView = _searchController.searchBar;
  40. self.definesPresentationContext = YES;
  41. }
  42. - (void)viewDidLoad
  43. {
  44. [super viewDidLoad];
  45. UIView *v = [[UIView alloc] initWithFrame:CGRectZero];
  46. [self.tableView setTableFooterView:v];
  47. self.clearsSelectionOnViewWillAppear = YES;
  48. self.tabBarController.tabBar.translucent = NO;
  49. self.navigationController.navigationBar.translucent = NO;
  50. self.view.backgroundColor = kBackGroundColor;
  51. [self addSearchController];
  52. }
  53. - (void)willPresentSearchController:(UISearchController *)searchController
  54. {
  55. [self unpinHeaderView];
  56. [searchController.searchResultsController layoutSubviewsFrame];
  57. }
  58. - (void)willDismissSearchController:(UISearchController *)searchController
  59. {
  60. [searchController.searchResultsController layoutSubviewsFrame];
  61. }
  62. @end