赞
踩
UISearchController是个坑啊,苹果为了,让开发者快速构架一块app,简化开发,私下添加太多东西,搞迷糊了。但是实际开发过程中,给的都需要重新去自定义,所以出现了很多莫名其妙的问题,究竟是不好呢,还是不好呢^_^
用UISearchController主要是为了hidesNavigationBarDuringPresentation这个属性后传来的动画效果,如果不用这个的话,我倒是很愿意直接用UISearchBar了
实现<UISearchControllerDelegate,UISearchResultsUpdating>
定义@property (nonatomic,strong) UISearchController *searchController;
//创建UISearchController
self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
//设置代理
self.searchController.delegate= self;
self.searchController.searchResultsUpdater = self;
//包着搜索框外层的颜色
// self.searchController.searchBar.barTintColor = [UIColor lig];
// self.searchController.searchBar.tintColor = [UIColor orangeColor];
//提醒字眼
self.searchController.searchBar.placeholder= @"搜索";
//提前在搜索框内加入搜索词
self.searchController.searchBar.text = @"";
//设置UISearchController的显示属性,以下3个属性默认为YES
//搜索时,背景变暗色
self.searchController.dimsBackgroundDuringPresentation = NO;
//搜索时,背景变模糊
// self.searchController.obscuresBackgroundDuringPresentation = YES;
//点击搜索的时候,是否隐藏导航栏
self.searchController.hidesNavigationBarDuringPresentation = YES;
//位置
[_searchController.searchBar sizeToFit];
self.tableView.tableHeaderView = self.searchController.searchBar;
// searchbar 样式
//设置背景图是为了去掉上下黑线
self.searchController.searchBar.backgroundImage =[UIImage imageWithColor:kWhiteColor size:CGSizeMake(kMinScreenWidth, 44)];
// 设置SearchBar的颜色主题为白色
self.searchController.searchBar.barTintColor = [UIColor whiteColor];
self.searchController.searchBar.delegate = self;
UITextField *searchField = [self.searchController.searchBar valueForKey:@"searchField"];
if (searchField) {
[searchField setBackgroundColor:kColorWithHex(@"f8f8f8")];
searchField.layer.cornerRadius = 14.0f;
// searchField.layer.borderColor = [UIColor colorWithRed:247/255.0 green:75/255.0 blue:31/255.0 alpha:1].CGColor;
// searchField.layer.borderWidth = 1;
searchField.layer.masksToBounds = YES;
}
//空搜索
UITextField *searchBarTextField = nil;
NSArray *views = ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0) ? self.searchController.searchBar.subviews : [[self.searchController.searchBar.subviews objectAtIndex:0] subviews];
for (UIView *subview in views)
{
if ([subview isKindOfClass:[UITextField class]])
{
searchBarTextField = (UITextField *)subview;
break;
}
}
searchBarTextField.enablesReturnKeyAutomatically = NO;
searchBarTextField.returnKeyType = UIReturnKeyDone;
self.definesPresentationContext=YES;
2.如果把searchBar当做tableView的headerVIew的使用,在方法- (void)willPresentSearchController:(UISearchController *)searchController
tableView已经reloadData了tableView.frame.y需要向下移动20个单位
CGRect tableFrame = self.tableView.frame;
tableFrame.origin.y = 20;
tableFrame.size.height = self.view.frame.size.height -20;
self.tableView.frame = tableFrame;
[UIView animateWithDuration:0.4 animations:^{
[self.view layoutIfNeeded];
[self.tableView layoutIfNeeded];
}];
3.如果还没有需要向上移动20个单位,然后再- (void)willDismissSearchController:(UISearchController *)searchController
让tableVIew会到原始位置
4.如果进入预编辑状态,searchBar消失(UISearchController套到TabBarController可能会出现这个情况),请添加下边这句话
- (void)didPresentSearchController:(UISearchController *)searchController
{
[self.view addSubview:self.searchController.searchBar];
}
5.如果UISearchController进入编辑状态后,导航栏遮挡住20加上
-(void)viewDidLayoutSubviews {
if(self.searchController.active) {
self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, 20, self.searchController.searchBar.frame.size.width, 44.0);
[UIView animateWithDuration:0.1 animations:^{
[self.view layoutIfNeeded];
[self.searchController.searchBar layoutIfNeeded];
}];
}
}
UISearchBar *searchBar = self.searchController.searchBar;
UIImageView *barImageView = [[[searchBar.subviews firstObject] subviews] firstObject];
barImageView.layer.borderColor = kWhiteColor.CGColor;
barImageView.layer.borderWidth = 1;
self.navigationController.navigationBar.translucent = YES;
不需要设置这些东西了
// self.definesPresentationContext=YES;
// self.edgesForExtendedLayout = UIRectEdgeNone;
// self.automaticallyAdjustsScrollViewInsets = NO;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。