2014-10-29 131 views
7

我試圖找出爲什麼,當我開始在UISearchController.searchBar IT負載正確輸入了我的整個導航欄消失,並激活正確的,但我失去了活躍的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

通知表視圖如何是存在的,但它似乎佔據的空間的導航控制器,並向下延伸到第一部分標題。有任何想法嗎?

謝謝。

+0

你解決這個問題? – eestein 2017-01-06 15:47:28

+0

發佈的解決方案下面,但我不知道究竟哪裏出了問題還在。 =] – 4m1r 2017-01-10 19:09:13

+0

NP,謝謝,我會看看那個 – eestein 2017-01-10 19:14:37

回答

2

您需要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 
0

這個工作對我來說

self.navigationController.navigationBar.translucent = true;

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

}