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

API中沒有陰影圖層的接口,嘗試分析解決
1、使用Reveal,查找遮蓋圖層,發現為_UISearchDisplayControllerDimmingView

2、找到該圖層,修改對應的frame,通過上圖可以發現dimmingview與searchResultsTableView為同一視圖的子圖層。
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
for(UIView * v in controller.searchResultsTableView.superview.subviews) { NSLog(@"%@",[v class]); if([v isKindOfClass:NSClassFromString(@"_UISearchDisplayControllerDimmingView")]) { v.frame = CGRectMake(0,20,320,400); // } }
}
3、通過以上代碼修改,遮罩圖層沒有在擋住搜索框了,但操作搜索框,還是發現搜索框的下區域不能獲取焦點,Cancel按鈕不方便使用。

4、通過上個圖可以看到雖然遮罩層下去了,但還是有個圖層在擋住,左邊列表該圖層顯示為searchResultsTableView的父視圖,再次修改代碼。
- (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); // } }}
5、搜索框可以正常使用。

6、同樣,如果需要調整searchResultsTableView的frame,在追加下面的代碼
- (void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView { tableView.frame =CGRectMake(0, 20, 320, 480-64-44);}
7、多次測試后發現,每次第一次執行無效,原因是由于searchDisplayControllerWillBeginSearch第一次執行時searchResultsTableView.superview為null,沒有添加到父視圖,可以使用兩種方案解決
方案一,延時執行:
- (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); // } }}
問題解決!
注:以上frame調整在ios7執行,ios7之前版本不需要,具體調整的frame大小可以根據自己的需求修改。
新聞熱點
疑難解答