服务器之家

服务器之家 > 正文

iOS之单独使用UISearchBar创建搜索框的示例

时间:2021-04-04 17:11     来源/作者:张大普奔

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

iOS之单独使用UISearchBar创建搜索框的示例

搜索页面

1.实现代理UISearchBarDelegate

?
1
@interface SearchViewController ()<UISearchBarDelegate>

2.创建一个UISearchBar为属性

?
1
@property (nonatomic, strong) UISearchBar *searchBar;

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

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

4.具体实现

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

5.实现代理方法

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

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

原文链接:http://www.jianshu.com/p/dd6f0a0b1781

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部