iOS之單獨(dú)使用UISearchBar創(chuàng)建搜索框的示例
這里實(shí)現(xiàn)的是進(jìn)入頁面后直接在導(dǎo)航欄上顯示搜索框(包含右側(cè)取消按鈕),并彈出鍵盤且搜索框?yàn)橹苯涌奢斎霠顟B(tài)(第一響應(yīng)者),點(diǎn)擊右側(cè)取消按鈕后收起鍵盤并返回上一頁。
搜索頁面
1.實(shí)現(xiàn)代理UISearchBarDelegate
@interface SearchViewController ()<UISearchBarDelegate>
2.創(chuàng)建一個(gè)UISearchBar為屬性
@property (nonatomic, strong) UISearchBar *searchBar;
3.進(jìn)入頁面后彈起鍵盤和離開頁面前收起鍵盤
- (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; if (!_searchBar.isFirstResponder) { [self.searchBar becomeFirstResponder]; } } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [self.searchBar resignFirstResponder]; }
4.具體實(shí)現(xiàn)
- (void)setBarButtonItem { //隱藏導(dǎo)航欄上的返回按鈕 [self.navigationItem setHidesBackButton:YES]; //用來放searchBar的View UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(5, 7, self.view.frame.size.width, 30)]; //創(chuàng)建searchBar UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(titleView.frame) - 15, 30)]; //默認(rèn)提示文字 searchBar.placeholder = @"搜索內(nèi)容"; //背景圖片 searchBar.backgroundImage = [UIImage imageNamed:@"clearImage"]; //代理 searchBar.delegate = self; //顯示右側(cè)取消按鈕 searchBar.showsCancelButton = YES; //光標(biāo)顏色 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"]; //設(shè)置按鈕上的文字 [cancleBtn setTitle:@"取消" forState:UIControlStateNormal]; //設(shè)置按鈕上文字的顏色 [cancleBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [titleView addSubview:searchBar]; self.searchBar = searchBar; self.navigationItem.titleView = titleView; }
5.實(shí)現(xiàn)代理方法
#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]; }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
簡單介紹iOS開發(fā)中關(guān)于category的應(yīng)用
這篇文章主要介紹了iOS開發(fā)中關(guān)于category的應(yīng)用,代碼仍然基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-09-09IOS打開照相機(jī)與本地相冊選擇圖片實(shí)例詳解
這篇文章主要介紹了IOS打開照相機(jī)與本地相冊選擇圖片實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06iOS安全防護(hù)系列之ptrace反調(diào)試與匯編調(diào)用系統(tǒng)方法詳解
這篇文章主要給大家介紹了關(guān)于iOS安全防護(hù)系列之ptrace反調(diào)試與匯編調(diào)用系統(tǒng)方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07IOS開發(fā)筆記整理49之詳解定位CLLocation
在項(xiàng)目功能中有一個(gè)定位CLLocation的需求,遇到了一些知識(shí)難點(diǎn),經(jīng)過各位大俠的幫助,問題解決,特此分享供大家學(xué)習(xí),希望大家共同學(xué)習(xí)進(jìn)步2015-11-11