iOS搜索栏和SeachDisplayController错位

iOS搜索栏和SeachDisplayController错位,ios,objective-c,searchbar,hamburger-menu,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 =

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

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;
结果是。。。我丢失了汉堡菜单按钮,无法更改搜索栏的宽度。我也有一个奇怪的差距,它看起来和我的导航栏一样大。我怎样才能拿回汉堡包菜单按钮并修复这个奇怪的缺口


将以下代码放入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;
}
注意:iOS 8中不推荐使用UISearchDisplayController

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

你也可以下载。

你在使用
UINavigationController
吗?@CsabiVidóI更新我的答案,请看一下。最后一个会更好!但我必须支持iOS 7+