在UISearchController文本字段中输入时,导航栏消失

我想知道为什么当我开始在UISearchController.searchBar中输入时,我的整个导航栏消失了。它正确加载并且动画正确,但是当我开始输入时,我失去了活动的NavBar。 以下是从viewDidLoad加载searchController的代码:

    UITableViewController *searchResultsController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];

    searchResultsController.tableView.dataSource = self;
    searchResultsController.tableView.delegate = self;
    searchResultsController.tableView.backgroundColor = [UIColor redColor];    

    self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
    self.searchController.delegate = self;
    self.searchController.searchResultsUpdater = self;
    self.searchController.dimsBackgroundDuringPresentation = YES;
    self.searchController.hidesNavigationBarDuringPresentation = NO;

    [self.searchController.searchBar sizeToFit];

    self.tableView.tableHeaderView = self.searchController.searchBar;
    self.definesPresentationContext = NO;

这是我开始输入后的结果:

在这里输入图像描述

注意表格视图是如何存在的,但它似乎占据了导航控制器的空间并向下延伸到第一部分标题。 有任何想法吗?

谢谢。


您需要将definesPresentationContext设置为YES。 从苹果的例子:

// Search is now just presenting a view controller. As such, normal view controller
// presentation semantics apply. Namely that presentation will walk up the view controller
// hierarchy until it finds the root view controller or one that defines a presentation context.

self.definesPresentationContext = YES

这项工作对我来说

self.navigationController.navigationBar.translucent = true;


所以,我最终能够用下面的init序列解决这个问题。 但是,我不太确定我做了什么来实现这一点。 现在很忙,所以我只需要粘贴这个解决方案,并且如果有人能够找到解决方案,就会接受解决方案。

- (void)loadSearchBar
{

    NSLog(@"Loading SearchBar");

    UITableViewController *searchResultsController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
    UITableView *myTv = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    searchResultsController.tableView = myTv;
    searchResultsController.tableView.dataSource = self;
    searchResultsController.tableView.delegate = self;
    searchResultsController.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    searchResultsController.tableView.sectionIndexColor = [UIColor colorWithHexString:linkBlue];

    self.searchResultsController = searchResultsController;

    self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
    self.searchController.delegate = self;
    self.searchController.searchResultsUpdater = self;
    self.searchController.dimsBackgroundDuringPresentation = YES;
    self.searchController.hidesNavigationBarDuringPresentation = NO;
    self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);

    self.searchBar = self.searchController.searchBar;
    self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
    self.searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
    self.searchBar.delegate = self;
    self.searchBar.translucent = YES;
    self.tableView.tableHeaderView.layer.zPosition++;

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

    //add color to search field
    UIView *subviews = [self.searchBar.subviews lastObject];
    UITextField *textView = (id)[subviews.subviews objectAtIndex:1];

    textView.backgroundColor = [UIColor colorWithHexString:textFieldBlue];

}
链接地址: http://www.djcxy.com/p/23299.html

上一篇: Navigation bar disappears when typing in UISearchController text field

下一篇: Google Plus sign in 'Choose an Account' dialog appears twice