首页 > 其他 > 详细

UISearchDisplayController 阴影遮盖frame调整

时间:2014-03-21 06:43:29      阅读:977      评论:0      收藏:0      [点我收藏+]

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

bubuko.com,布布扣

 

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

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

bubuko.com,布布扣

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

1
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller<br>{ 
1
2
3
4
5
6
7
8
for(UIView * v in controller.searchResultsTableView.superview.subviews)
 {
     NSLog(@"%@",[v class]);
     if([v isKindOfClass:NSClassFromString(@"_UISearchDisplayControllerDimmingView")])
     {
         v.frame = CGRectMake(0,20,320,400); //
     }
 }
1
<br>}  <br> 

 3、通过以上代码修改,遮罩图层没有在挡住搜索框了,但操作搜索框,还是发现搜索框的下区域不能获取焦点,Cancel按钮不方便使用。

bubuko.com,布布扣

4、通过上个图可以看到虽然遮罩层下去了,但还是有个图层在挡住,左边列表该图层显示为searchResultsTableView的父视图,再次修改代码。

bubuko.com,布布扣
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
    
    controller.searchResultsTableView.superview.bounds = CGRectMake(0,22,320,400);
    for(UIView * v in controller.searchResultsTableView.superview.subviews)
    {
        NSLog(@"%@",[v class]);
        if([v isKindOfClass:NSClassFromString(@"_UISearchDisplayControllerDimmingView")])
        {
            v.frame = CGRectMake(0,20,320,400); //
        }
    }
}
bubuko.com,布布扣

 5、搜索框可以正常使用。

bubuko.com,布布扣

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

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

 7、多次测试后发现,每次第一次执行无效,原因是由于searchDisplayControllerWillBeginSearch第一次执行时searchResultsTableView.superview为null,没有添加到父视图,可以使用两种方案解决

方案一,延时执行:

bubuko.com,布布扣
- (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); //
        }
    }
}
bubuko.com,布布扣

方案二,注册键盘通知:

bubuko.com,布布扣
- (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); //
        }
    }
}
bubuko.com,布布扣

 

问题解决!

注:以上frame调整在ios7执行,ios7之前版本不需要,具体调整的frame大小可以根据自己的需求修改。

UISearchDisplayController 阴影遮盖frame调整,布布扣,bubuko.com

UISearchDisplayController 阴影遮盖frame调整

原文:http://www.cnblogs.com/geweb/p/UISearchDisplayControllerDimmingView.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!