SearchBaseVC.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. //
  2. // SearchBaseVC.m
  3. // smartRhino
  4. //
  5. // Created by niuzhen on 2020/1/1.
  6. // Copyright © 2020 tederen. All rights reserved.
  7. //
  8. #import "SearchBaseVC.h"
  9. #import "SearchCell.h"
  10. @interface SearchBaseVC ()<UISearchBarDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
  11. @property (strong, nonatomic) UISearchBar * searchBar;
  12. @property (strong, nonatomic) UIView * searchV;
  13. @property (strong, nonatomic) UIButton * backBtn;
  14. @property (strong, nonatomic) UIView * navBarV;
  15. @property (strong, nonatomic) UILabel * lineL;
  16. @property (nonatomic, strong) NSMutableArray * dataDistoryArray;
  17. @end
  18. @implementation SearchBaseVC
  19. - (UIView *)noDataView
  20. {
  21. if (!_noDataView) {
  22. _noDataView = [UIView new];
  23. _noDataView.hidden = YES;
  24. _noDataView.backgroundColor = [UIColor whiteColor];
  25. }
  26. return _noDataView;
  27. }
  28. - (UILabel *)noDataL
  29. {
  30. if (!_noDataL) {
  31. _noDataL = [UILabel new];
  32. _noDataL.textColor = UIColorHex(#BBBBBB);
  33. _noDataL.font = [UIFont systemFontOfSize:15];
  34. }
  35. return _noDataL;
  36. }
  37. - (UIButton *)cancelBtn
  38. {
  39. if (!_cancelBtn) {
  40. _cancelBtn = [UIButton new];
  41. [_cancelBtn setAttributedTitle:[[NSMutableAttributedString alloc] initWithString:@"取消" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFang SC" size: 15],NSForegroundColorAttributeName: UIColorHex(#3979D3)}] forState:UIControlStateNormal];
  42. }
  43. return _cancelBtn;
  44. }
  45. - (UIButton *)backBtn
  46. {
  47. if (!_backBtn) {
  48. _backBtn = [UIButton new];
  49. [_backBtn setImage:IMG(@"back_black_icon") forState:UIControlStateNormal];
  50. }
  51. return _backBtn;
  52. }
  53. - (UILabel *)lineL
  54. {
  55. if (!_lineL) {
  56. _lineL = [UILabel new];
  57. _lineL.backgroundColor = LINEBGCOLOR;
  58. }
  59. return _lineL;
  60. }
  61. - (UIView *)navBarV
  62. {
  63. if (!_navBarV) {
  64. _navBarV = [UIView new];
  65. _navBarV.backgroundColor = [UIColor whiteColor];
  66. }
  67. return _navBarV;
  68. }
  69. - (UISearchBar *)searchBar
  70. {
  71. if (!_searchBar) {
  72. _searchBar = [UISearchBar new];
  73. }
  74. return _searchBar;
  75. }
  76. - (UICollectionView *)collectionView
  77. {
  78. if (!_collectionView) {
  79. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
  80. [_collectionView registerClass:[SearchCell class] forCellWithReuseIdentifier:@"cellId"];
  81. }
  82. return _collectionView;
  83. }
  84. - (UITableView *)tableView
  85. {
  86. if (!_tableView) {
  87. _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  88. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  89. }
  90. return _tableView;
  91. }
  92. - (void)setHistorySearchType:(HistorySearchType)historySearchType
  93. {
  94. _historySearchType = historySearchType;
  95. self.dataDistoryArray = [[_TDFileStorage loadHistorySearchBySearchType:_historySearchType] mutableCopy];
  96. }
  97. - (void)viewDidLoad {
  98. [super viewDidLoad];
  99. self.fd_prefersNavigationBarHidden = YES;
  100. [self setCusView];
  101. self.collectionView.hidden = NO;
  102. self.tableView.hidden = YES;
  103. self.collectionView.delegate = self;
  104. self.collectionView.dataSource = self;
  105. [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headReusableView"];
  106. self.collectionView.backgroundColor = [UIColor whiteColor];
  107. self.view.backgroundColor = RGB(255, 255, 255);
  108. WS(weakSelf);
  109. [self.backBtn setAction:^{
  110. if (weakSelf.cancelBtn.isHidden) {
  111. [weakSelf.navigationController popViewControllerAnimated:YES];
  112. }else{
  113. weakSelf.collectionView.hidden = NO;
  114. weakSelf.noDataView.hidden = YES;
  115. weakSelf.tableView.hidden = YES;
  116. [weakSelf upCanCelBtnConstraits];
  117. }
  118. }];
  119. [self.cancelBtn setAction:^{
  120. weakSelf.searchBar.text = @"";
  121. weakSelf.collectionView.hidden = NO;
  122. weakSelf.noDataView.hidden = YES;
  123. weakSelf.tableView.hidden = YES;
  124. [weakSelf upCanCelBtnConstraits];
  125. }];
  126. [self.searchBar becomeFirstResponder];
  127. }
  128. - (void)setCusView
  129. {
  130. [self.view addSubview:self.navBarV];
  131. [self.navBarV mas_makeConstraints:^(MASConstraintMaker *make) {
  132. make.top.left.right.mas_equalTo(self.view);
  133. make.height.offset(IS_IPHONEX ? 84:64);
  134. }];
  135. [self.navBarV addSubview:self.backBtn];
  136. [self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  137. make.left.bottom.mas_equalTo(self.navBarV);
  138. make.size.mas_offset(CGSizeMake(50, 44));
  139. }];
  140. [self.navBarV addSubview:self.cancelBtn];
  141. [self upCanCelBtnConstraits];
  142. [self.navBarV addSubview:self.searchBar];
  143. [self.searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
  144. make.left.mas_equalTo(self.backBtn.mas_right);
  145. make.centerY.mas_equalTo(self.backBtn);
  146. make.height.mas_offset(32);
  147. make.right.mas_equalTo(self.cancelBtn.mas_left).offset(-15);
  148. }];
  149. [self.navBarV addSubview:self.lineL];
  150. [self.lineL mas_makeConstraints:^(MASConstraintMaker *make) {
  151. make.left.bottom.right.mas_equalTo(self.navBarV);
  152. make.height.mas_offset(0.5);
  153. }];
  154. self.searchBar.layer.borderWidth = 0.5f;
  155. self.searchBar.layer.cornerRadius = 3.f;
  156. self.searchBar.layer.borderColor = UIColorHex(#CCCCCC).CGColor;
  157. self.searchBar.layer.masksToBounds = YES;
  158. [self.view addSubview:self.collectionView];
  159. [self.view addSubview:self.tableView];
  160. [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  161. make.top.mas_equalTo(self.navBarV.mas_bottom);
  162. make.left.right.mas_equalTo(self.view);
  163. if (@available(iOS 11.0, *)) {
  164. make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
  165. } else {
  166. make.bottom.equalTo(self.view.mas_bottom);
  167. }
  168. }];
  169. [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  170. make.top.mas_equalTo(self.navBarV.mas_bottom);
  171. make.left.right.mas_equalTo(self.view);
  172. if (@available(iOS 11.0, *)) {
  173. make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
  174. } else {
  175. make.bottom.equalTo(self.view.mas_bottom);
  176. }
  177. }];
  178. [self.view addSubview:self.noDataView];
  179. self.noDataView.hidden = YES;
  180. [self.noDataView mas_makeConstraints:^(MASConstraintMaker *make) {
  181. make.top.mas_equalTo(self.navBarV.mas_bottom);
  182. make.left.right.mas_equalTo(self.view);
  183. if (@available(iOS 11.0, *)) {
  184. make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
  185. } else {
  186. make.bottom.equalTo(self.view.mas_bottom);
  187. }
  188. }];
  189. [self.noDataView addSubview:self.noDataL];
  190. [self.noDataL mas_makeConstraints:^(MASConstraintMaker *make) {
  191. make.center.mas_equalTo(self.noDataView);
  192. }];
  193. _searchBar.backgroundImage = [UtilsTools imageWithColor:[UIColor clearColor] size:_searchBar.bounds.size];
  194. UIImage* clearImg = [UtilsTools imageWithColor:[UIColor clearColor] andHeight:44.0f];
  195. [_searchBar setBackgroundImage:clearImg];
  196. [_searchBar setSearchFieldBackgroundImage:clearImg forState:UIControlStateNormal];
  197. [_searchBar setBackgroundColor:[UIColor clearColor]];
  198. [_searchV setBackgroundColor:[UIColor whiteColor]];
  199. [_searchV setRadius:4 borderColor:RGB(204, 204, 204) borderWidth:.5];
  200. self.searchBar.delegate = self;
  201. if ([UIDevice currentDevice].systemVersion.floatValue >= 13.0) {
  202. self.searchBar.searchTextField.font = [UIFont systemFontOfSize:14.0];
  203. if (!self.searchBar.searchTextField.placeholder) {
  204. self.searchBar.searchTextField.placeholder = @"搜索";
  205. }
  206. NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:self.searchBar.searchTextField.placeholder attributes: @{NSForegroundColorAttributeName:RGB(153, 153, 153), NSFontAttributeName:self.searchBar.searchTextField.font}];
  207. self.searchBar.searchTextField.attributedPlaceholder = attrString;
  208. }else{
  209. for (UIView *view in self.searchBar.subviews.lastObject.subviews) {
  210. if([view isKindOfClass:NSClassFromString(@"UISearchBarTextField")]) {
  211. UITextField *textField = (UITextField *)view;
  212. //设置输入框的背景颜色
  213. textField.clipsToBounds = YES;
  214. //设置输入字体颜色
  215. textField.textColor = [UIColor blackColor];
  216. //设置默认文字颜色
  217. textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@" 搜索" attributes:@{NSForegroundColorAttributeName:[UIColor hexStringToColor:@"999999"]}];
  218. textField.font = [UIFont systemFontOfSize:14];
  219. for (UIView * subView in view.subviews) {
  220. if ([subView isKindOfClass:NSClassFromString(@"UIImageView")]) {
  221. UIImageView * imageV = (UIImageView *)subView;
  222. imageV.image = IMG(@"searchIcon");
  223. }
  224. }
  225. }
  226. }
  227. }
  228. }
  229. - (NSMutableArray *)dataDistoryArray
  230. {
  231. if (!_dataDistoryArray) {
  232. _dataDistoryArray = [NSMutableArray array];
  233. }
  234. return _dataDistoryArray;
  235. }
  236. /**********************************************************************/
  237. #pragma mark -UICollectionViewDataSource
  238. /**********************************************************************/
  239. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
  240. if (self.dataDistoryArray.count == 0) {
  241. return 0;
  242. }
  243. return 1;
  244. }
  245. /**********************************************************************/
  246. #pragma mark -GHRefreshCollectionView的section的Count
  247. /**********************************************************************/
  248. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  249. return self.dataDistoryArray.count;
  250. }
  251. #pragma mark - UICollectionViewDelegate
  252. - (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
  253. SearchCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
  254. cell.titleL.text = [self.dataDistoryArray objectAtIndex:indexPath.row];
  255. return cell;
  256. }
  257. -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
  258. return CGSizeMake(SCREEN_WIDTH, 40);
  259. }
  260. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  261. [collectionView deselectItemAtIndexPath:indexPath animated:YES];
  262. [self selectIndexText:[self.dataDistoryArray objectAtIndex:indexPath.row]];
  263. [self showCancelBtn];
  264. }
  265. //footer的size
  266. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
  267. {
  268. return CGSizeMake(0, 0);
  269. }
  270. //header的size
  271. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
  272. {
  273. return CGSizeMake(SCREEN_WIDTH, 44);
  274. }
  275. //设置每个item的UIEdgeInsets
  276. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  277. {
  278. return UIEdgeInsetsMake(0, 0, 0, 0);
  279. }
  280. //设置每个item水平间距
  281. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
  282. {
  283. return 0.f;
  284. }
  285. //设置每个item垂直间距
  286. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
  287. {
  288. return 0.f;
  289. }
  290. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
  291. if([kind isEqualToString:UICollectionElementKindSectionHeader]){
  292. UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headReusableView" forIndexPath:indexPath];
  293. [headerView removeAllSubviews];
  294. UILabel *label = [[UILabel alloc] init];
  295. [headerView addSubview:label];
  296. [label mas_makeConstraints:^(MASConstraintMaker *make) {
  297. make.centerY.equalTo(headerView);
  298. make.left.equalTo(@(15));
  299. }];
  300. label.text = @"搜索历史";
  301. label.font = [UIFont systemFontOfSize:16];
  302. label.textColor = UIColorHex(#333333);
  303. UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  304. [headerView addSubview:closeBtn];
  305. [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  306. make.centerY.equalTo(headerView);
  307. make.right.equalTo(@(-15));
  308. }];
  309. [closeBtn setTitle:@"清空" forState:UIControlStateNormal];
  310. [closeBtn setTitleColor:UIColorHex(#278BDD) forState:UIControlStateNormal];
  311. closeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
  312. WS(weakSelf);
  313. [closeBtn setAction:^{
  314. [weakSelf.dataDistoryArray removeAllObjects];
  315. [_TDFileStorage saveHistorySearchBySearchType:self.historySearchType WithArr:@[]];
  316. [weakSelf.collectionView reloadData];
  317. }];
  318. headerView.backgroundColor = [UIColor whiteColor];
  319. return headerView;
  320. }
  321. return nil;
  322. }
  323. - (void)selectIndexText:(NSString *)text
  324. {
  325. self.tableView.hidden = NO;
  326. self.collectionView.hidden = YES;
  327. self.searchBar.text = text;
  328. self.searchText = self.searchBar.text;
  329. }
  330. - (void)checkRepeatString:(NSString *)string {
  331. for (NSString *cache in self.dataDistoryArray) {
  332. if ([cache isEqualToString:string]) {
  333. return;
  334. }
  335. }
  336. [self.dataDistoryArray insertObject:string atIndex:0];
  337. [_TDFileStorage saveHistorySearchBySearchType:self.historySearchType WithArr:self.dataDistoryArray];
  338. [self.collectionView reloadData];
  339. }
  340. /**********************************************************************/
  341. #pragma mark - UISearchBarDelegate
  342. /**********************************************************************/
  343. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
  344. }
  345. - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
  346. {
  347. if (searchBar.text.length > 0) {
  348. [self selectIndexText:searchBar.text];
  349. [self checkRepeatString:searchBar.text];
  350. [self showCancelBtn];
  351. }
  352. }
  353. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
  354. }
  355. - (void)searchBarTextDidBeginEditing:(UISearchBar *) searchBar{
  356. UITextField *searchBarTextField = nil;
  357. NSArray *views = ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0) ? searchBar.subviews : [[searchBar.subviews objectAtIndex:0] subviews];
  358. for (UIView *subview in views){
  359. if ([subview isKindOfClass:[UITextField class]]){
  360. searchBarTextField = (UITextField *)subview;
  361. break;
  362. }
  363. }
  364. searchBarTextField.enablesReturnKeyAutomatically = NO;
  365. }
  366. - (void)showCancelBtn
  367. {
  368. if (self.cancelBtn.hidden) {
  369. self.cancelBtn.hidden = NO;
  370. [self.cancelBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
  371. make.right.mas_equalTo(self.navBarV.mas_right).offset(-15);
  372. make.centerY.mas_equalTo(self.backBtn);
  373. }];
  374. }
  375. }
  376. - (void)upCanCelBtnConstraits
  377. {
  378. self.cancelBtn.hidden = YES;
  379. [self.cancelBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
  380. make.right.mas_equalTo(self.navBarV.mas_right);
  381. make.centerY.mas_equalTo(self.backBtn);
  382. make.width.mas_offset(0);
  383. }];
  384. }
  385. - (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope{
  386. NSLog(@"%@1",searchBar.text);
  387. }
  388. @end