點選搜尋進入預編譯狀態,searbar跟隨導航消失
阿新 • • 發佈:2018-11-02
注意:如果出現下邊這種情況
Paste_Image.png
解決方式:在ViewDidLoad新增以下程式碼
- (void)viewDidLoad {
#warning 如果進入預編輯狀態,searchBar消失(UISearchController套到TabBarController可能會出現這個情況),請新增下邊這句話
self.definesPresentationContext=YES;
}
實現效果
Paste_Image.png
iOS8之前,使用搜索採用的是:UISearchBar+UIDisplayController;
iOS8之後,採用的是UISearchController,看演示+上程式碼:
demo演示圖
//添加了tableView的代理是為了顯示搜尋的列表 @interface SearchViewController ()<UITableViewDelegate,UITableViewDataSource, UISearchControllerDelegate,UISearchResultsUpdating> //searchController @property (nonatomic,retain) UISearchController *searchController; //tableView @property (nonatomic,strong) UITableView *skTableView; //資料來源 @property (nonatomic,strong) NSMutableArray *dataListArry; @property (nonatomic,strong) NSMutableArray *searchListArry; @end
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"搜尋列表";
self.dataListArry = [NSMutableArray array];
self.searchListArry = [NSMutableArray array];
self.dataListArry = [NSMutableArray arrayWithCapacity:100];
//產生100個數字+三個隨機字母
for (NSInteger i =0; i<100; i++) {
[self.dataListArry addObject:[NSString stringWithFormat:@"%ld%@",(long)i,[self shuffledAlphabet]]];
}
self.skTableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width ,[UIScreen mainScreen].bounds.size.height)];
self.skTableView.delegate = self;
self.skTableView.dataSource = self;
//隱藏tableViewCell下劃線
// self.skTableView.separatorStyle = UITableViewCellSelectionStyleNone;
//建立UISearchController
self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
//設定代理
self.searchController.delegate= self;
self.searchController.searchResultsUpdater = self;
//包著搜尋框外層的顏色
self.searchController.searchBar.barTintColor = [UIColor yellowColor];
//提醒字眼
self.searchController.searchBar.placeholder= @"請輸入關鍵字搜尋";
//提前在搜尋框內加入搜尋詞
self.searchController.searchBar.text = @"我是周杰倫";
//設定UISearchController的顯示屬性,以下3個屬性預設為YES
//搜尋時,背景變暗色
self.searchController.dimsBackgroundDuringPresentation = NO;
//搜尋時,背景變模糊
// self.searchController.obscuresBackgroundDuringPresentation = NO;
//點選搜尋的時候,是否隱藏導航欄
// self.searchController.hidesNavigationBarDuringPresentation = NO;
//位置
self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
// 新增 searchbar 到 headerview
self.skTableView.tableHeaderView = self.searchController.searchBar;
[self.view addSubview: self.skTableView];
}
//產生3個隨機字母
- (NSString *)shuffledAlphabet {
NSMutableArray * shuffledAlphabet = [NSMutableArray arrayWithArray:@[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z"]];
NSString *strTest = [[NSString alloc]init];
for (int i=0; i<3; i++) {
int x = arc4random() % 25;
strTest = [NSString stringWithFormat:@"%@%@",strTest,shuffledAlphabet[x]];
}
return strTest;
}
//設定區域的行數
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (self.searchController.active) {
return [self.searchListArry count];
}
else{
return [self.dataListArry count];
}
}
//返回單元格內容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString * [email protected]"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:flag];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:flag];
}
if (self.searchController.active) {
[cell.textLabel setText:self.searchListArry[indexPath.row]];
}
else{
[cell.textLabel setText:self.dataListArry[indexPath.row]];
}
return cell;
}
//謂詞搜尋過濾
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
// //修改"Cancle"退出字眼,這樣修改,按鈕一開始就直接出現,而不是搜尋的時候再出現
// searchController.searchBar.showsCancelButton = YES;
// for(id sousuo in [searchController.searchBar subviews])
// {
//
// for (id zz in [sousuo subviews])
// {
//
// if([zz isKindOfClass:[UIButton class]]){
// UIButton *btn = (UIButton *)zz;
// [btn setTitle:@"搜尋" forState:UIControlStateNormal];
// [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// }
//
//
// }
// }
NSLog(@"updateSearchResultsForSearchController");
NSString *searchString = [self.searchController.searchBar text];
NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];
if (self.searchListArry!= nil) {
[self.searchListArry removeAllObjects];
}
//過濾資料
self.searchListArry= [NSMutableArray arrayWithArray:[self.dataListArry filteredArrayUsingPredicate:preicate]];
//重新整理表格
[self.skTableView reloadData];
}
#pragma mark - UISearchControllerDelegate代理,可以省略,主要是為了驗證列印的順序
//測試UISearchController的執行過程
- (void)willPresentSearchController:(UISearchController *)searchController
{
NSLog(@"willPresentSearchController");
}
- (void)didPresentSearchController:(UISearchController *)searchController
{
NSLog(@"didPresentSearchController");
#warning 如果進入預編輯狀態,searchBar消失(UISearchController套到TabBarController可能會出現這個情況),請新增下邊這句話
// [self.view addSubview:self.searchController.searchBar];
}
- (void)willDismissSearchController:(UISearchController *)searchController
{
NSLog(@"willDismissSearchController");
}
- (void)didDismissSearchController:(UISearchController *)searchController
{
NSLog(@"didDismissSearchController");
}
- (void)presentSearchController:(UISearchController *)searchController
{
NSLog(@"presentSearchController");
}
demo地址: https://github.com/OwenJoe/UISearchController.git