UISearchDisplayController的一些总结

iOS7.0****使用****UISearchDisplayController****阴影遮盖****frame****调整

iOS7中UISearchDisplayController 与UISearchBar结合使用时,有时候会出现搜索框获得焦点时,阴影遮盖部分挡住了搜索框,影响用户使用,如下图

UISearchDisplayController的一些总结_第1张图片
Pasted Graphic.png

API中没有阴影图层的接口,尝试分析解决

1、使用Reveal,查找遮盖图层,发现为_UISearchDisplayControllerDimmingView

UISearchDisplayController的一些总结_第2张图片
Pasted Graphic 1.png

2、找到该图层,修改对应的frame,通过上图可以发现dimmingview与searchResultsTableView为同一视图的子图层。

// 不要用searchDisplayControllerWillBeiginSearch,因为第一次搜索的时候,不会有效果,不信可以试一下
// 试验:第一次点击search,没有效果,然后点击蒙版,再点击search,才会出现
// searchDisplayControllerWillBeginSearch每次第一次执行无效,原因是由于第一次执行时searchResultsTableView.superview为null,没有添加到父视图
- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller {
// 第一种遍历
    for(UIView * v in controller.searchResultsTableView.superview.subviews)
    {
        if([v isKindOfClass:[NSClassFromString(@"_UISearchDisplayControllerDimmingView") class]])
        {
            v.frame = CGRectMake(0,20,320,400); //
            NSLog(@"--------- %@",[v class]);
        }
    }
    

// 第二种遍历
    UIView *supV = controller.searchResultsTableView.superview;
    UIView *supsupV = supV.superview;
    
    for (UIView *view in supsupV.subviews) {
        for (UIView *sencondView in view.subviews) {
            if ([sencondView isKindOfClass:[NSClassFromString(@"_UISearchDisplayControllerDimmingView") class]])
            {
                NSLog(@"_UISearchDisplayControllerDimmingView");
                //                if (![sencondView viewWithTag:99]) {
                //                    [sencondView addSubview:yourCustomView];
                //                }
                sencondView.alpha = 1;
            }
        }
    }
}

3、同样,如果需要调整searchResultsTableView的frame,在追加下面的代码

- (void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView
{
    tableView.frame =CGRectMake(0, 20, 320, 480-64-44);
}

4、如果非想用searchDisplayControllerWillBeginSearch
方案一,延时执行:

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller{
    NSLog(@"%@,%@",self.searchDisplayController.searchResultsTableView,self.searchDisplayController.searchResultsTableView.superview);
    [self performSelector:@selector(resetFrame) withObject:nil afterDelay:0.1];
}
- (void)resetFrame{
    CGRect bounds =  self.searchDisplayController.searchResultsTableView.superview.bounds;
    CGFloat offset = CGRectGetMinY(bounds);
    if (offset == 0)
    {
        self.searchDisplayController.searchResultsTableView.superview.bounds =CGRectMake(0,20,320,400);
    }
    for(UIView * v in self.searchDisplayController.searchResultsTableView.superview.subviews)
    {
        NSLog(@"%@",[v class]);
        if([v isKindOfClass:NSClassFromString(@"_UISearchDisplayControllerDimmingView")])
        {
            v.frame = CGRectMake(0,20,320,400);
        }
    }
}

方案二,注册键盘通知:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resetFrame)                                                 name:UIKeyboardWillShowNotification object:nil];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
}
- (void)resetFrame{
    CGRect bounds =  self.searchDisplayController.searchResultsTableView.superview.bounds;
    CGFloat offset = CGRectGetMinY(bounds);
    if (offset == 0)
    {
        self.searchDisplayController.searchResultsTableView.superview.bounds =CGRectMake(0,20,320,400);
    }
    for(UIView * v in self.searchDisplayController.searchResultsTableView.superview.subviews)
    {
        NSLog(@"%@",[v class]);
        if([v isKindOfClass:NSClassFromString(@"_UISearchDisplayControllerDimmingView")])
        {
            v.frame = CGRectMake(0,20,320,400);
        }
    }
}

iOS7.0使用UISearchDisplayController的搜索的返回结果让tablevie的frame变化

出现这种状态是因为键盘的frame导致的UITableView的frame发生变化

解决:

- (void)searchDisplayController:(UISearchDisplayController *)controller didHideSearchResultsTableView:(UITableView *)tableView {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    
}

- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView {
    
    tableView.backgroundColor = [UIColor colorWithRed:0.902 green:0.918 blue:0.941 alpha:1.000];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide) name:UIKeyboardWillHideNotification object:nil];
    
}

- (void) keyboardWillHide {
    
    UITableView *tableView = [[self searchDisplayController] searchResultsTableView];
    
    [tableView setContentInset:UIEdgeInsetsZero];
    
    [tableView setScrollIndicatorInsets:UIEdgeInsetsZero];
    
}

实际上的解决办法是:

-(void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView
{
    
    [tableView setContentInset:UIEdgeInsetsZero];
    [tableView setScrollIndicatorInsets:UIEdgeInsetsZero];
    
    /*
    tableView.backgroundColor = [UIColor colorWithRed:0.902 green:0.918 blue:0.941 alpha:1.000];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    tableView.showsVerticalScrollIndicator = NO;
    */
}

你可能感兴趣的:(UISearchDisplayController的一些总结)