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

时间:2014-10-29 17:42:22

标签: uitableview ios8 uinavigationbar 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;

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

enter image description here

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

感谢。

2 个答案:

答案 0 :(得分:0)

这项工作对我来说

self.navigationController.navigationBar.translucent = true;

答案 1 :(得分:0)

所以,我最终能够通过以下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];

}
相关问题