怎么在iOS中使用UISearchBar創(chuàng)建一個(gè)搜索框?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
創(chuàng)新互聯(lián)建站專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站建設(shè)、網(wǎng)站制作、霍州網(wǎng)絡(luò)推廣、微信小程序、霍州網(wǎng)絡(luò)營(yíng)銷、霍州企業(yè)策劃、霍州品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們大的嘉獎(jiǎng);創(chuàng)新互聯(lián)建站為所有大學(xué)生創(chuàng)業(yè)者提供霍州建站搭建服務(wù),24小時(shí)服務(wù)熱線:18980820575,官方網(wǎng)址:www.cdcxhl.com搜索頁面
1.實(shí)現(xiàn)代理UISearchBarDelegate
@interface SearchViewController ()
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)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)建站的支持。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站www.cdcxhl.com,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、建站服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。