简体   繁体   English

导航栏上的SearchController在iOS11上不可见

[英]SearchController on Navigation Bar is invisible on iOS11

Until iOS 11 was released, I had a search bar added to a UITableViewController. 在发布iOS 11之前,我已将搜索栏添加到UITableViewController。 It is hidden under a navigation bar and is visible when user scrolls the table up. 它隐藏在导航栏下,当用户向上滚动表格时可见。 On iOS11, adjusting contentOffset or tableview.bound won't hide the search bar properly, sometimes it does work and sometimes it hides the first row, too. 在iOS11上,调整contentOffset或tableview.bound不会正确隐藏搜索栏,有时它可以工作,有时也隐藏第一行。 So, I've decided to move the search bar to the navigation bar. 因此,我决定将搜索栏移动到导航栏。 But, for some reason, I don't see it anywhere. 但是,由于某种原因,我在任何地方都看不到它。 Here's my code at viewDidLoad 这是我在viewDidLoad的代码

YearTableViewController.m

- (void)viewDidLoad
{

[super viewDidLoad];


if (@available(iOS 11.0, *)) {

//iOS11 -> on navigation bar
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.delegate = self;
self.searchController.searchBar.delegate = self;
self.searchController.searchBar.barStyle = UISearchBarStyleDefault;
self.searchController.searchBar.showsCancelButton = NO;
[self.searchController.searchBar sizeToFit];
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.dimsBackgroundDuringPresentation = YES;
[self.navigationItem setSearchController:self.searchController];
[self.navigationController.navigationBar setPrefersLargeTitles:NO];
[self.navigationItem setLargeTitleDisplayMode:UINavigationItemLargeTitleDisplayModeNever];
self.definesPresentationContext = YES;


} else {

//iOS10 or previous -> on table view
UITableViewController *tableViewControllerForSearch = [[UITableViewController alloc]initWithStyle:UITableViewStylePlain];
tableViewControllerForSearch.tableView.dataSource = self;
tableViewControllerForSearch.tableView.delegate = self;
tableViewControllerForSearch.tableView.estimatedRowHeight = 40;
tableViewControllerForSearch.tableView.rowHeight = UITableViewAutomaticDimension;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:tableViewControllerForSearch];
self.searchController.searchResultsUpdater = self;
self.searchController.delegate = self;
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.tableView.tableHeaderView = self.searchController.searchBar;
self.definesPresentationContext = YES;
// No border between cells
tableViewControllerForSearch.tableView.separatorStyle = UITableViewCellSelectionStyleNone;

}

EDITED: 2017/10/20 As suggested, I've renamed and simplified the structure. 编辑:2017/10/20如建议,我已重命名并简化了结构。

I have the following structure on storyboard. 我在情节提要上具有以下结构。 UINavigationController: Initial view controller YearPageViewController: Inside, it has YearTableViewController UINavigationController:初始视图控制器YearPageViewController:内部,它具有YearTableViewController

On iOS10, it works fine. 在iOS10上,它可以正常工作。 On iOS 11, I don't see the searchBar. 在iOS 11上,我看不到searchBar。 Thanks for your input. 感谢您的输入。

What about adding 怎么样添加

    self.navigationItem.hidesSearchBarWhenScrolling = NO;

Obviously the search bar is hidden by default and only is to be seen when scrolling. 显然,搜索栏默认情况下是隐藏的,只有在滚动时才能看到。

Hope will helpful to you On iOS11 希望对您有帮助

if #available(iOS 11.0, *) {
     self.navigationItem.hidesSearchBarWhenScrolling = NO; 
} 

I known why your view controller is stacks. 我知道为什么您的视图控制器是堆栈。 set ' NO' flag in dimsBackgroundDuringPresentation dimsBackgroundDuringPresentation中设置“ 否”标志

self.searchController.dimsBackgroundDuringPresentation = NO;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM