@interface SearchViewController ()<UISearchBarDelegate>
@property (nonatomic, strong) UISearchBar *searchBar;
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if (!_searchBar.isFirstResponder) {
[self.searchBar becomeFirstResponder];
}
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.searchBar resignFirstResponder];
}
- (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;
}
#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];
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有