在 UISearchController 文本字段中键入时导航栏消失
Posted
技术标签:
【中文标题】在 UISearchController 文本字段中键入时导航栏消失【英文标题】:Navigation bar disappears when typing in UISearchController text field 【发布时间】:2014-10-29 17:42:21 【问题描述】:我试图弄清楚为什么当我开始在 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;
这是我开始输入后的结果:
注意表格视图是如何存在的,但它似乎占据了导航控制器的空间并向下延伸到第一节标题。有什么想法吗?
谢谢。
【问题讨论】:
你解决了吗? 在下面发布了解决方案,但我不确定问题出在哪里。 =] np,谢谢,我去看看 【参考方案1】:您需要将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
【讨论】:
但它也说,“一些系统提供的视图控制器,例如 UINavigationController,将默认值更改为 true。”那么如果它嵌入到导航控制器中,为什么它仍然会消失呢?【参考方案2】:这对我有用
self.navigationController.navigationBar.translucent = true;
【讨论】:
【参考方案3】:所以,我最终能够通过以下初始化序列解决这个问题。但是,我不太确定我做了什么来实现这一点。现在有点忙,所以我将粘贴此解决方案,如果有人可以查明,我将接受解决方案。
- (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];
【讨论】:
以上是关于在 UISearchController 文本字段中键入时导航栏消失的主要内容,如果未能解决你的问题,请参考以下文章
UISearchController 在关闭时清除 UISearchBar 文本