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;
*/
}