SearchTabBaseVC.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. //
  2. // SearchTabBaseVC.m
  3. // smartRhino
  4. //
  5. // Created by niuzhen on 2020/4/8.
  6. // Copyright © 2020 tederen. All rights reserved.
  7. //
  8. #import "SearchTabBaseVC.h"
  9. #import "SearchTabCell.h"
  10. @interface SearchTabBaseVC ()<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 SearchTabBaseVC
  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:[SearchTabCell class] forCellWithReuseIdentifier:@"SearchTabCellId"];
  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. SearchTabCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SearchTabCellId" 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. UILabel * label = [UILabel new];
  259. label.font = [UIFont systemFontOfSize:13];
  260. label.text = [self.dataDistoryArray objectAtIndex:indexPath.row];
  261. CGFloat width = [label sizeThatFits:CGSizeMake(SCREEN_WIDTH, 25)].width;
  262. if (width > SCREEN_WIDTH * 0.7) {
  263. width = SCREEN_WIDTH * 0.7;
  264. }
  265. return CGSizeMake(width + 22, 25);
  266. }
  267. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  268. [collectionView deselectItemAtIndexPath:indexPath animated:YES];
  269. [self selectIndexText:[self.dataDistoryArray objectAtIndex:indexPath.row]];
  270. [self showCancelBtn];
  271. }
  272. //footer的size
  273. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
  274. {
  275. return CGSizeMake(0, 0);
  276. }
  277. //header的size
  278. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
  279. {
  280. return CGSizeMake(SCREEN_WIDTH, 44);
  281. }
  282. //设置每个item的UIEdgeInsets
  283. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  284. {
  285. return UIEdgeInsetsMake(0, 15, 0, 15);
  286. }
  287. //设置每个item水平间距
  288. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
  289. {
  290. return 11.f;
  291. }
  292. //设置每个item垂直间距
  293. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
  294. {
  295. return 9.f;
  296. }
  297. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
  298. if([kind isEqualToString:UICollectionElementKindSectionHeader]){
  299. UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headReusableView" forIndexPath:indexPath];
  300. [headerView removeAllSubviews];
  301. UILabel *label = [[UILabel alloc] init];
  302. [headerView addSubview:label];
  303. [label mas_makeConstraints:^(MASConstraintMaker *make) {
  304. make.centerY.equalTo(headerView);
  305. make.left.equalTo(@(15));
  306. }];
  307. label.text = @"搜索历史";
  308. label.font = [UIFont systemFontOfSize:16];
  309. label.textColor = UIColorHex(#333333);
  310. UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  311. [headerView addSubview:closeBtn];
  312. [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  313. make.centerY.equalTo(headerView);
  314. make.right.equalTo(@(-15));
  315. }];
  316. [closeBtn setTitle:@"清空" forState:UIControlStateNormal];
  317. [closeBtn setTitleColor:UIColorHex(#278BDD) forState:UIControlStateNormal];
  318. closeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
  319. WS(weakSelf);
  320. [closeBtn setAction:^{
  321. [weakSelf.dataDistoryArray removeAllObjects];
  322. [_TDFileStorage saveHistorySearchBySearchType:self.historySearchType WithArr:@[]];
  323. [weakSelf.collectionView reloadData];
  324. }];
  325. headerView.backgroundColor = [UIColor whiteColor];
  326. return headerView;
  327. }
  328. return nil;
  329. }
  330. - (void)selectIndexText:(NSString *)text
  331. {
  332. self.tableView.hidden = NO;
  333. self.collectionView.hidden = YES;
  334. self.searchBar.text = text;
  335. self.searchText = self.searchBar.text;
  336. }
  337. - (void)checkRepeatString:(NSString *)string {
  338. for (NSString *cache in self.dataDistoryArray) {
  339. if ([cache isEqualToString:string]) {
  340. return;
  341. }
  342. }
  343. [self.dataDistoryArray insertObject:string atIndex:0];
  344. [_TDFileStorage saveHistorySearchBySearchType:self.historySearchType WithArr:self.dataDistoryArray];
  345. [self.collectionView reloadData];
  346. }
  347. /**********************************************************************/
  348. #pragma mark - UISearchBarDelegate
  349. /**********************************************************************/
  350. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
  351. }
  352. - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
  353. {
  354. if (searchBar.text.length > 0) {
  355. [self selectIndexText:searchBar.text];
  356. [self checkRepeatString:searchBar.text];
  357. [self showCancelBtn];
  358. }
  359. }
  360. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
  361. }
  362. - (void)searchBarTextDidBeginEditing:(UISearchBar *) searchBar{
  363. UITextField *searchBarTextField = nil;
  364. NSArray *views = ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0) ? searchBar.subviews : [[searchBar.subviews objectAtIndex:0] subviews];
  365. for (UIView *subview in views){
  366. if ([subview isKindOfClass:[UITextField class]]){
  367. searchBarTextField = (UITextField *)subview;
  368. break;
  369. }
  370. }
  371. searchBarTextField.enablesReturnKeyAutomatically = NO;
  372. }
  373. - (void)showCancelBtn
  374. {
  375. if (self.cancelBtn.hidden) {
  376. self.cancelBtn.hidden = NO;
  377. [self.cancelBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
  378. make.right.mas_equalTo(self.navBarV.mas_right).offset(-15);
  379. make.centerY.mas_equalTo(self.backBtn);
  380. }];
  381. }
  382. }
  383. - (void)upCanCelBtnConstraits
  384. {
  385. self.cancelBtn.hidden = YES;
  386. [self.cancelBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
  387. make.right.mas_equalTo(self.navBarV.mas_right);
  388. make.centerY.mas_equalTo(self.backBtn);
  389. make.width.mas_offset(0);
  390. }];
  391. }
  392. - (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope{
  393. NSLog(@"%@1",searchBar.text);
  394. }
  395. @end