iOS SearchBar 和 SeachDisplayController 错位

标签 ios objective-c searchbar hamburger-menu

我需要使用搜索栏,但我使用此代码:

self.searchBar = [UISearchBar new];
_searchBar.frame = CGRectMake(0, 0, 200, 44);
_searchBar.searchBarStyle = UISearchBarStyleMinimal;
_searchBar.placeholder = @"Search stores";
_searchBar.delegate = self;

self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
_searchController.searchResultsDataSource = self;
_searchController.searchResultsDelegate = self;
_searchController.delegate = self;

self.definesPresentationContext = YES;
self.edgesForExtendedLayout = UIRectEdgeNone;

结果是这样的...我丢失了汉堡菜单按钮,并且无法更改搜索栏的宽度。我也有一个奇怪的间隙,它似乎和我的导航栏一样大。我怎样才能找回汉堡菜单按钮并修复奇怪的间隙?

enter image description here enter image description here

最佳答案

将以下代码放入ViewDidLoad中。它适用于 iOS 7+

 if(SYSTEM_VERSION_GREATER_THAN(@"6.1")) {
       self.edgesForExtendedLayout = UIRectEdgeNone;
  }

已编辑

 - (void)setActive:(BOOL)visible animated:(BOOL)animated
{
    [super setActive:visible animated:animated];

    [self.searchContentsController.navigationController setNavigationBarHidden: NO animated: NO];

    CGRect frame = self.searchResultsTableView.frame;
    frame.origin.y = CGRectGetHeight(self.searchContentsController.navigationController.navigationBar.frame);

    frame.size.height = CGRectGetHeight(frame) - CGRectGetMinY(frame);

    self.searchResultsTableView.frame = frame;

    frame = self.searchBar.frame;
    self.searchBar.frame = frame;

    [self.searchContentsController.view insertSubview:self.searchBar aboveSubview:self.searchResultsTableView];

}

再尝试实现此解决方案。

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
    self.navigationController.navigationBar.translucent = YES;
}

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {
    self.navigationController.navigationBar.translucent = NO;
}

注意:UISearchDisplayController 在 iOS 8 中已弃用。Apple Document

在iOS8中使用以下代码支持

searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
searchController.searchResultsUpdater = self;
searchController.dimsBackgroundDuringPresentation = NO;
searchController.hidesNavigationBarDuringPresentation = NO;
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;

您还可以下载the sample code from here .

关于iOS SearchBar 和 SeachDisplayController 错位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30509355/

相关文章:

ios - 将 SKSpriteNode 掩蔽为液体

ios - 基于自定义 .xib 的 CollectionView 超出预期范围

ios - 使用 UIView 作为 UIButton 的背景?

objective-c - Collection was mutated while being enumerated 错误在 Objective-C

IOS 6 - 自定义 UIButton 如何给我的 titleLabel 留出边距?

ios - UIScrollView圆顶效果

objective-c - 如何在 iPad 应用程序中显示 Microsoft SQL Server Reporting Services 报告?

iOS Rxswift 处理 CancelButton 在 searchBar 中点击

django - 如何通过基于类的 View 添加搜索栏

ios - 带有 SearchBar 的 UITableViewController