UISearchDisplayController的一些總結

iOS7.0****使用****UISearchDisplayController****陰影遮蓋****frame****調整

iOS7中UISearchDisplayController 與UISearchBar結合使用時,有時候會出現搜索框獲得焦點時,陰影遮蓋部分擋住了搜索框,影響用戶使用,如下圖

Pasted Graphic.png

API中沒有陰影圖層的接口,嘗試分析解決

1、使用Reveal,查找遮蓋圖層,發(fā)現為_UISearchDisplayControllerDimmingView

Pasted Graphic 1.png

2、找到該圖層,修改對應的frame,通過上圖可以發(fā)現dimmingview與searchResultsTableView為同一視圖的子圖層。

// 不要用searchDisplayControllerWillBeiginSearch,因為第一次搜索的時候,不會有效果,不信可以試一下
// 試驗:第一次點擊search,沒有效果,然后點擊蒙版,再點擊search,才會出現
// searchDisplayControllerWillBeginSearch每次第一次執(zhí)行無效,原因是由于第一次執(zhí)行時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
方案一,延時執(zhí)行:

- (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變化

出現這種狀態(tài)是因為鍵盤的frame導致的UITableView的frame發(fā)生變化

解決:

- (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;
    */
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 發(fā)現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 15,355評論 4 61
  • 在iOS中隨處都可以看到絢麗的動畫效果,實現這些動畫的過程并不復雜,今天將帶大家一窺ios動畫全貌。在這里你可以看...
    每天刷兩次牙閱讀 8,694評論 6 30
  • 1.導入第三方庫CryptoSwiftimport CryptoSwiftCryptoSwift源碼地址 DEMO...
    嗯_挺好的閱讀 687評論 1 2
  • 背景 在開發(fā)過程中,有時候需要對瀏覽器環(huán)境進行檢測,比如封裝一個AJAX函數的時候需要寫一個函數進行檢測,但是常規(guī)...
    Binaryify閱讀 1,550評論 0 1
  • 1,assumptions假設有下列這些特征:隱藏或者沒有明說出來作者認為是理所當然的對判斷其結論有較大影響可能有...
    人間有阿九閱讀 1,902評論 0 0

友情鏈接更多精彩內容