UISearchController dimsBackgroundDuringPresentation仅在搜索文本为空时

时间:2015-11-18 19:47:18

标签: ios objective-c uisearchbar uisearchcontroller

我有UISearchController和UITableView。 viewDidLoad中的代码是:

self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = YES;
[self.searchController.searchBar sizeToFit];
self.searchController.searchBar.delegate = self;
self.searchController.delegate = self;

self.tableView.tableHeaderView = self.searchController.searchBar;
self.tableView.userInteractionEnabled = YES;

我希望每当我点击搜索栏时都会出现灰色视图,当我开始输入时,灰色视图会消失并显示tableView,这样我就可以点按这些单元格。这意味着仅当搜索栏为空时才会显示灰色视图(就像邮件和联系人应用程序中的默认搜索行为一样)。我试着设置

self.searchController.dimsBackgroundDuringPresentation 

在基于searchBar.text

的委托方法中
-(void )searchBarTextDidBeginEditing:(UISearchBar *)searchBar

但它不起作用。 有什么想法吗?

谢谢,

3 个答案:

答案 0 :(得分:3)

如果您为searchResultsController使用另一个视图控制器,则

self.searchController.dimsBackgroundDuringPresentation = YES非常有用但在您的代码中,您使用当前视图来显示结果([[UISearchController alloc] initWithSearchResultsController:nil])。

我希望每当我点击搜索栏时都会出现灰色视图,当我开始输入时,灰色视图会消失并显示tableView,这样我就可以点按这些单元格。

如果您正在为searchResultsController使用另一个视图控制器,则这是默认行为。

答案 1 :(得分:1)

当表格显示并设置灰色和Alpha时,我为表添加了subView。 当Dismiss SearchController删除子视图时。我将dim属性设置为false。 我的代码如下,它可能会帮助你。 我使用相同的表格来显示搜索结果。

// on header file
UIView *dimView = null; 

//on .m file

       // create DimView for SearchControl
    - (void)showDimView
    {
        if(dimView == nil && self.searchController.active)
        {
            CGRect rcReplacementView = self.tableView.frame;
            dimView = [[UIView alloc] initWithFrame:rcReplacementView];
            dimView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
            dimView.backgroundColor  = [UIColor blackColor];
            dimView.alpha = 0.5;
            [self.view addSubview:dimView];
            self.tableView.scrollEnabled = NO;

            //tap event for hide seachcontroll 
            UITapGestureRecognizer *singleFingerTap =
            [[UITapGestureRecognizer alloc] initWithTarget:self
                                                    action:@selector(handleSingleTap:)];
            [dimView addGestureRecognizer:singleFingerTap];
            [singleFingerTap release];
        }
    }

//close SearchController if Tap on view
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {

    if(searchController.searchBar.text.length <= 0)
    {
        [self.searchController setActive:NO];
    }
}

// do something before the search controller is dismissed
- (void)willDismissSearchController:(UISearchController *)searchController {

    if(dimView != nil)
    {
        [dimView removeFromSuperview];
        dimView = nil;
    }
    self.tableView.scrollEnabled = YES;
}

答案 2 :(得分:0)

一种骇人听闻的方法是在初始化 UISearchController 时将 UISearchController 背景设置为浅灰色颜色并设置 dimsBackgroundDuringPresentation obscuresBackgroundDuringPresentation 设置为 false ,然后在 textDidChange 委托方法中将背景更改为清除。

(void)viewDidLoad()
{
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    self.searchController.searchBar.placeholder = NSLocalizedString(@"Search", @"");
    self.searchController.view.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.2f];
    self.searchController.dimsBackgroundDuringPresentation = false;
    [searchController.searchBar sizeToFit];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    NSUInteger length = [searchText length];
    if (length > 0)
    {
        self.searchController.view.backgroundColor = [UIColor clearColor];
        [self.searchController.view reloadInputViews];
    } else {
        self.searchController.view.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.2f];
        [self.searchController.view reloadInputViews];
    }

}