【问题标题】:UISearchController dimsBackgroundDuringPresentation only when the search text emptyUISearchController dimsBackgroundDuringPresentation 仅当搜索文本为空时
【发布时间】:2015-11-18 19:47:17
【问题描述】:

我有 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

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

谢谢,

【问题讨论】:

  • 这就是 dimsBackgroundDuringPresentation 的工作原理。你是如何实现 uisearchcontroller 的?它只会使搜索结果变暗。看起来你可能不会展示任何东西。
  • 这就是我为 UISearchController 所做的一切。其他一切都由表视图实现,并且 (void)updateSearchResultsForSearchController:(UISearchController *)searchController 根据搜索文本重新加载 tabla 视图。我还有什么需要做的吗?
  • 不幸的是,它比这更复杂。您需要搜索 tableview 的数据源并找到搜索结果,然后将它们呈现给用户。您可以在此处找到有关如何执行此操作的教程:jhof.me/simple-uisearchcontroller-implementation
  • @beyowulf 是的,我按照教程进行了操作,并且成功了。谢谢。
  • 但是应该有办法动态设置 dimsBackgroundDuringPresentation 属性,对你来说写新表很容易,但是如果表很复杂,许多开发人员更喜欢使用同一个控制器来显示结果,怎么办这种情况?问题依然存在。

标签: ios objective-c uisearchbar uisearchcontroller


【解决方案1】:

self.searchController.dimsBackgroundDuringPresentation = YES 在您使用另一个视图控制器时很有用 searchResultsController 但在您的代码中,您使用当前视图来显示结果 ([[UISearchController alloc] initWithSearchResultsController:nil])。

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

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

【讨论】:

  • 我认为Apple应该通过异常禁用这种奇怪而无用的效果,或者可能是日志中的警告......我的意思是如果你选择searchResultsController作为当前(nil),那么dimsBackgroundDuringPresentation永远不应该是YES。
【解决方案2】:

当表格显示并设置灰色和Alpha时,我为表格添加了子视图。 当 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;
}

【讨论】:

  • 谢谢,我更喜欢苹果的标准实现。
【解决方案3】:

一种 hacky 方法是在初始化 UISearchController 时将 UISearchController 背景设置为 浅灰色 并设置 dimsBackgroundDuringPresentationobscuresBackgroundDuringPresentationfalse,然后在 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];
    }

}

【讨论】:

    猜你喜欢
    • 2015-08-26
    • 2021-06-28
    • 2012-07-24
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2023-03-09
    • 1970-01-01
    相关资源
    最近更新 更多