怎么在iOS中使用UISearchBar创建一个搜索框?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
创新互联建站专注为客户提供全方位的互联网综合服务,包含不限于成都网站建设、网站制作、霍州网络推广、微信小程序、霍州网络营销、霍州企业策划、霍州品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们大的嘉奖;创新互联建站为所有大学生创业者提供霍州建站搭建服务,24小时服务热线:18980820575,官方网址:www.cdcxhl.com搜索页面
1.实现代理UISearchBarDelegate
@interface SearchViewController ()
2.创建一个UISearchBar为属性
@property (nonatomic, strong) UISearchBar *searchBar;
3.进入页面后弹起键盘和离开页面前收起键盘
- (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; if (!_searchBar.isFirstResponder) { [self.searchBar becomeFirstResponder]; } } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [self.searchBar resignFirstResponder]; }
4.具体实现
- (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.实现代理方法
#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]; }
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联网站建设公司行业资讯频道,感谢您对创新互联建站的支持。
另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、建站服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
售后响应及时
7×24小时客服热线数据备份
更安全、更高效、更稳定价格公道精准
项目经理精准报价不弄虚作假合作无风险
重合同讲信誉,无效全额退款