10

我试图弄清楚为什么当我开始在 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;

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

在此处输入图像描述

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

谢谢。

4

3 回答 3

8

您需要将定义PresentationContext 设置为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
于 2015-09-09T21:24:17.143 回答
0

所以,我最终能够用下面的初始化序列解决这个问题。但是,我不太确定我做了什么来实现这一点。现在有点忙,所以我将粘贴此解决方案,如果有人可以查明,我将接受解决方案。

- (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];

}
于 2017-01-10T19:08:14.163 回答
0

这对我有用

self.navigationController.navigationBar.translucent = true;

于 2016-05-05T11:47:45.207 回答