UISearchController 很坑
Posted xuanInitial
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UISearchController 很坑相关的知识,希望对你有一定的参考价值。
最近一个项目用到了很多搜索 踩了一大波坑 还要继续踩下去
直接上代码了
<UISearchControllerDelegate,UISearchResultsUpdating> //声明 @property (nonatomic,retain) UISearchController *searchController; - (void)createUI{ self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil]; self.searchController.delegate= self; self.searchController.searchResultsUpdater = self; self.searchController.searchBar.barTintColor = BHBackColor; self.searchController.searchBar.placeholder= @"车牌号"; self.searchController.searchBar.searchBarStyle =UISearchBarStyleMinimal; // //提前在搜索框内加入搜索词 // self.searchController.searchBar.text = @"xxxx"; self.searchController.searchBar.tintColor = BHNoticeColor; //搜索时,背景变暗色 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); self.definesPresentationContext = YES; self.searchController.hidesNavigationBarDuringPresentation = NO; [self.searchController.searchBar setContentMode:UIViewContentModeLeft]; self.navigationItem.titleView =self.searchController.searchBar; UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil]; self.navigationItem.rightBarButtonItem = rightBarItem; [self.view addSubview:self.searchTableView]; } //谓词搜索过滤 -(void)updateSearchResultsForSearchController:(UISearchController *)searchController { NSLog(@"搜索"); NSString *searchString = [self.searchController.searchBar text]; //下面处理数据 怎么处理的都可以 这个方法是 searchbar 内容改变一次就调用一次 }
想要在跳转页面的时候键盘直接弹出来
- (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self performSelector:@selector(showKeyboard) withObject:nil afterDelay:0]; } - (void)showKeyboard { [self.searchController.searchBar becomeFirstResponder]; }
如果
self.searchController.hidesNavigationBarDuringPresentation = YES; //默认是YES
不要在
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController{ //不要加 self.edgesForExtendedLayout = UIRectEdgeNone; }
否则会在返回的时候上移 44个像素
连接涉及到的坑
iOS双击Home键进入switcher任务管理切屏退出进入闪屏的解决办法
测试结果 第一种和第二种不可用 如果有加搜索的话取消按钮也会变成透明的 第三种 [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(1000, 1000) forBarMetrics:UIBarMetricsDefault];
其他待更新......
//搜索框
- (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; if (!_searchBar.isFirstResponder) { [self.searchBar becomeFirstResponder]; } } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; // 回收键盘 [self.searchBar resignFirstResponder]; }
- (void)setBarButtonItem { [self.navigationItem setHidesBackButton:YES]; // 创建搜索框 UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(5, 7, self.view.frame.size.width, 30)]; UIImage *img = [UIImage imageNamed:@"arrow_back_gray"]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(-10, 10, 22, 22); [btn setImage:img forState:UIControlStateNormal]; [btn addTarget:self action:@selector(presentVCFirstBackClick:) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *bbiBack = [[UIBarButtonItem alloc] initWithCustomView:btn]; self.navigationItem.leftBarButtonItem = bbiBack; UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(titleView.frame) - 40, 30)]; searchBar.placeholder = @"车牌号"; searchBar.backgroundImage = [UIImage imageNamed:@"clearImage"]; searchBar.delegate = self; searchBar.showsCancelButton = YES; searchBar.autocorrectionType = UITextAutocorrectionTypeNo; UIView *searchTextField = searchTextField = [searchBar valueForKey:@"_searchField"]; searchTextField.backgroundColor = [UIColor colorWithRed:234/255.0 green:235/255.0 blue:237/255.0 alpha:1]; [searchBar setImage:[UIImage imageNamed:@"sort_magnifier"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal]; UIButton *cancleBtn = [searchBar valueForKey:@"cancelButton"]; //修改标题和标题颜色 [cancleBtn setTitle:@"取消" forState:UIControlStateNormal]; [cancleBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [titleView addSubview:searchBar]; self.searchBar = searchBar; [self.searchBar becomeFirstResponder]; self.navigationItem.titleView = titleView; self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:nil]; } - (void)presentVCFirstBackClick:(UIButton *)sender { [_searchBar resignFirstResponder]; [self.navigationController popToRootViewControllerAnimated:YES]; } - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { [self.searchBar resignFirstResponder]; } - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar { searchBar.showsCancelButton = YES; } - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { }
以上是关于UISearchController 很坑的主要内容,如果未能解决你的问题,请参考以下文章
如何使用代码将 UISearchController Searchbar 放到导航栏上