国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS之單獨使用UISearchBar創建搜索框的示例

2020-07-26 02:38:27
字體:
來源:轉載
供稿:網友

這里實現的是進入頁面后直接在導航欄上顯示搜索框(包含右側取消按鈕),并彈出鍵盤且搜索框為直接可輸入狀態(第一響應者),點擊右側取消按鈕后收起鍵盤并返回上一頁。

搜索頁面

1.實現代理UISearchBarDelegate

@interface SearchViewController ()<UISearchBarDelegate>

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];}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 北川| 曲松县| 双鸭山市| 东乡族自治县| 湟中县| 师宗县| 南郑县| 东丰县| 宁乡县| 康平县| 长沙市| 盐津县| 新龙县| 张家界市| 思南县| 桂平市| 红河县| 如皋市| 晋中市| 开阳县| 木里| 香河县| 罗田县| 格尔木市| 会昌县| 潞西市| 贺兰县| 台南县| 普兰店市| 蕲春县| 沧州市| 渑池县| 五原县| 崇文区| 府谷县| 涿州市| 凌源市| 土默特左旗| 永兴县| 灌南县| 临城县|