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

Uitableview 在UISearchController文本字段中键入时,导航栏消失,uitableview,ios8,uinavigationbar,uisearchcontroller,Uitableview,Ios8,Uinavigationbar,Uisearchcontroller,我试图弄清楚为什么当我开始在UISearchController.searchBar中键入时,我的整个导航栏会消失。它正确加载并正确设置动画,但当我开始键入时,我会丢失活动的导航栏。下面是从viewDidLoad加载searchController的代码: UITableViewController *searchResultsController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];

我试图弄清楚为什么当我开始在UISearchController.searchBar中键入时,我的整个导航栏会消失。它正确加载并正确设置动画,但当我开始键入时,我会丢失活动的导航栏。下面是从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;
这是我开始打字后的结果:

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


谢谢。

您需要将DefinePresentationContext设置为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];

}

您解决了这个问题吗?在下面发布了解决方案,但我不确定问题到底出在哪里。=]np,谢谢,我会看一看,但它也会说,“一些系统提供的视图控制器,如UINavigationController,会将默认值更改为true。”因此,如果它嵌入到导航控制器中,为什么它仍然会消失?