查看: 449|回复: 0

[IOS开发教程] iOS之单独使用UISearchBar创建搜索框的示例

发表于 2017-11-29 08:00:04

这里实现的是进入页面后直接在导航栏上显示搜索框(包含右侧取消按钮),并弹出键盘且搜索框为直接可输入状态(第一响应者),点击右侧取消按钮后收起键盘并返回上一页。

搜索页面

1.实现代理UISearchBarDelegate

  1. @interface SearchViewController ()<UISearchBarDelegate>
复制代码

2.创建一个UISearchBar为属性

  1. @property (nonatomic, strong) UISearchBar *searchBar;
复制代码

3.进入页面后弹起键盘和离开页面前收起键盘

  1. - (void)viewDidAppear:(BOOL)animated
  2. {
  3. [super viewDidAppear:animated];
  4. if (!_searchBar.isFirstResponder) {
  5. [self.searchBar becomeFirstResponder];
  6. }
  7. }
  8. - (void)viewWillDisappear:(BOOL)animated
  9. {
  10. [super viewWillDisappear:animated];
  11. [self.searchBar resignFirstResponder];
  12. }
复制代码

4.具体实现

  1. - (void)setBarButtonItem
  2. {
  3. //隐藏导航栏上的返回按钮
  4. [self.navigationItem setHidesBackButton:YES];
  5. //用来放searchBar的View
  6. UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(5, 7, self.view.frame.size.width, 30)];
  7. //创建searchBar
  8. UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(titleView.frame) - 15, 30)];
  9. //默认提示文字
  10. searchBar.placeholder = @"搜索内容";
  11. //背景图片
  12. searchBar.backgroundImage = [UIImage imageNamed:@"clearImage"];
  13. //代理
  14. searchBar.delegate = self;
  15. //显示右侧取消按钮
  16. searchBar.showsCancelButton = YES;
  17. //光标颜色
  18. searchBar.tintColor = UIColorFromRGB(0x595959);
  19. //拿到searchBar的输入框
  20. UITextField *searchTextField = [searchBar valueForKey:@"_searchField"];
  21. //字体大小
  22. searchTextField.font = [UIFont systemFontOfSize:15];
  23. //输入框背景颜色
  24. searchTextField.backgroundColor = [UIColor colorWithRed:234/255.0 green:235/255.0 blue:237/255.0 alpha:1];
  25. //拿到取消按钮
  26. UIButton *cancleBtn = [searchBar valueForKey:@"cancelButton"];
  27. //设置按钮上的文字
  28. [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
  29. //设置按钮上文字的颜色
  30. [cancleBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  31. [titleView addSubview:searchBar];
  32. self.searchBar = searchBar;
  33. self.navigationItem.titleView = titleView;
  34. }
复制代码

5.实现代理方法

  1. #pragma mark - UISearchBarDelegate
  2. - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
  3. return YES;
  4. }
  5. - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
  6. searchBar.showsCancelButton = YES;
  7. }
  8. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
  9. {
  10. NSLog(@"SearchButton");
  11. }
  12. - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
  13. {
  14. [self.searchBar resignFirstResponder];
  15. [self.navigationController popViewControllerAnimated:YES];
  16. }
  17. - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
  18. {
  19. searchBar.showsCancelButton = YES;
  20. }
  21. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
  22. {
  23. NSString *inputStr = searchText;
  24. [self.results removeAllObjects];
  25. for (ElderModel *model in self.dataArray) {
  26. if ([model.name.lowercaseString rangeOfString:inputStr.lowercaseString].location != NSNotFound) {
  27. [self.results addObject:model];
  28. }
  29. }
  30. [self.tableView reloadData];
  31. }
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。



回复

使用道具 举报

关闭

站长推荐上一条 /1 下一条