#import "ViewController.h"
#define KScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
static NSString *identifier = @"CellID";
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchResultsUpdating,UISearchBarDelegate>{
UITableView *mTableView;
NSMutableArray *filteredNames;
UISearchController *searchController;
}
@property (nonatomic,strong) NSDictionary *names;
@property (nonatomic,strong) NSArray *keys;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
float statusBarHeight = 0;
if (@available(iOS 13.0,*)){
// iOS 13 弃用keyWindow属性 从所有windowl数组中取
UIStatusBarManager *statusManager = [UIApplication sharedApplication].windows.firstObject.windowScene.statusBarManager;
statusBarHeight = statusManager.statusBarFrame.size.height;
NSLog(@"statusBarHeight1:%f",statusBarHeight);
}
else{
statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
NSLog(@"statusBarHeight2:%f",statusBarHeight);
}
mTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, statusBarHeight, KScreenWidth,kScreenHeight-statusBarHeight) style:UITableViewStylePlain];
mTableView.delegate = self;
mTableView.dataSource = self;
[self.view addSubview:mTableView];
[mTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:identifier];
filteredNames = [NSMutableArray array];
searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
//设置背景不透明
searchController.searchBar.translucent = NO;
// 改变searchBar背景颜色
// searchController.searchBar.barTintColor = [UIColor brownColor];
//设置searchbar的边框颜色和背景颜色一致
//设置UISearchController的显示属性,以下属性默认为YES
// searchController.searchBar.layer.borderWidth = 1;
// searchController.searchBar.layer.borderColor = [UIColor brownColor].CGColor;
searchController.searchBar.placeholder = @"搜索联系人";
//当输入框激活状态,整个界面会添加一个半透明的view ,默认是添加的
searchController.obscuresBackgroundDuringPresentation = YES;
// 当输入框激活的状态,会隐藏导航条,默认状态是隐藏的
searchController.hidesNavigationBarDuringPresentation = YES;
searchController.searchBar.delegate = self;
searchController.searchResultsUpdater = self;
// 改变取消按钮字体颜色
// searchController.searchBar.tintColor = [UIColor redColor];
// 取消searchBar上下边缘的分割线
// searchController.searchBar.backgroundImage = [[UIImage alloc]init];
// 有时候弹出的搜索VC的搜索框会先弹出屏幕外一下,然后又弹回来,可以试试这一句代码
self.definesPresentationContext = YES;
// searchController.active = NO;
mTableView.tableHeaderView = searchController.searchBar;
NSString *path = [[NSBundle mainBundle]pathForResource:@"sortednames" ofType:@"plist"];
self.names = [NSDictionary dictionaryWithContentsOfFile:path];
self.keys = [[self.names allKeys]sortedArrayUsingSelector:@selector(compare:)];
}
#pragma mark - UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//控制器使用的时候,就是点击了搜索框的时候
if (searchController.active) {
return 1;
}
return self.keys.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//控制器使用的时候,就是点击了搜索框的时候
if (searchController.active) {
return [filteredNames count];
}
NSString *key = self.keys[section];
NSArray *nameSection = self.names[key];
return nameSection.count;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//控制器使用的时候,就是点击了搜索框的时候
if (searchController.active) {
return nil;
}
return self.keys[section];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
//控制器使用的时候,就是点击了搜索框的时候
if (searchController.active) {
cell.textLabel.text = filteredNames[indexPath.row];
}
else{
NSString *key = self.keys[indexPath.section];
NSArray *nameSection = self.names[key];
cell.textLabel.text = nameSection[indexPath.row];
}
return cell;
}
- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
//控制器使用的时候,就是点击了搜索框的时候
if (searchController.active) {
return nil;
}
return self.keys;
}
// 输入内容发生变化,会调用此方法
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{
// searchController.active = NO;
NSString *searchText = searchController.searchBar.text;
[filteredNames removeAllObjects];
if (searchText.length>0) {
NSLog(@"搜索内容:%@",searchText);
NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
NSRange range = [evaluatedObject rangeOfString:searchText options:NSCaseInsensitiveSearch];
return range.location != NSNotFound;
}];
for (NSString *key in self.keys) {
NSArray *matches = [self.names[key] filteredArrayUsingPredicate:predicate];
[filteredNames addObjectsFromArray:matches];
};
[mTableView reloadData];
};
}
-(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
//修改"Cancle"退出字眼,这样修改,按钮一开始就直接出现,而不是搜索的时候再出现
[searchBar setShowsCancelButton:YES animated:YES];
// 注意searchBar的button处于孙图层中
for (UIView *view1 in [searchBar subviews]) {
if ([view1 isKindOfClass:[UIView class]]) {
for (UIView *view2 in [view1 subviews]) {
if(@available(iOS 13.0, *)) {
if ([view2 isKindOfClass:NSClassFromString(@"_UISearchBarSearchContainerView")]){
//ios13输入文本框又往里移了一层
for (UIView *view3 in [view2 subviews]){
if ([view3 isKindOfClass:[UIButton class]]) {
UIButton *btn = (UIButton *)view3;
[btn setTitle:@"取消" forState:UIControlStateNormal];
}
}
}
}else{
if ([view2 isKindOfClass:[UIButton class]]) {
UIButton *btn = (UIButton *)view2;
[btn setTitle:@"取消" forState:UIControlStateNormal];
}
}
}
}
}
return YES;
}
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
searchController.active = NO;
[mTableView reloadData];
}