当进入结果视图并返回时,带有UISearchController的UITableView进入导航栏

时间:2015-10-08 09:15:32

标签: ios objective-c uitableview uinavigationbar uisearchcontroller

我在UITableView中有一个UISearchController UINavigationBar个搜索栏,一切正常,但是当我推送UISearchController的搜索结果时,我回来了UITableView在NavBar之下,这是我初始化UISearchController的方式:

        self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
        self.searchController.delegate = self;
        self.searchController.searchResultsUpdater = self;

        self.searchController.searchBar.delegate = self;
        self.searchController.dimsBackgroundDuringPresentation = NO;
        self.searchController.hidesNavigationBarDuringPresentation = NO;
        self.searchController.searchBar.placeholder = NSLocalizedString(@"Local Search", @"");
        self.searchController.searchBar.frame = CGRectMake(0, -5, [UIScreen mainScreen].bounds.size.width, 44);


        ctrl = [[UIView alloc] initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 44)];
        ctrl.backgroundColor = [UIColor clearColor];
        ctrl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        [ctrl addSubview:self.searchController.searchBar];
        self.navigationItem.titleView = ctrl;
        self.definesPresentationContext = YES;

搜索栏在UINavigationBar中完美显示,然后当我搜索某些内容时,我按下了一个结果的视图控制器:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    DetailListController *detailList = [[DetailListController alloc] init];
    [self.navigationController pushViewController:detailList animated:YES];
}

当我回到UITableView这样做:

[self.navigationController popViewControllerAnimated:YES];

UITableView位于UINavigationBar之下,我该如何解决这个问题?

感谢

5 个答案:

答案 0 :(得分:10)

我遇到了完全相同的问题,在视图控制器中将extendedLayoutIncludesOpaqueBars设置为YES / true,它提供了搜索控制器,似乎可以修复它。

self.extendedLayoutIncludesOpaqueBars = YES

请注意:设置此属性会更改滚动视图的垂直内容偏移值。

答案 1 :(得分:9)

这是UIKit中一个非常令人沮丧的错误。似乎正在发生的事情是呈现视图控制器的顶部布局指南重置为0,这意味着它现在位于导航栏下方。布局指南是一个只读属性,因此您无法通过直接编辑来修复它。但是,我确实提出了一个hack来让它重置为正确的值。将其添加到您的UISearchControllerDelegate

- (void)didDismissSearchController:(UISearchController *)searchController
{
    UINavigationController *nav = self.navController; // you muse save this earlier

    // force to layout guide to reset by pushing a dummy controller and popping it right back
    [nav pushViewController:[UIViewController new] animated:NO];
    [nav popViewControllerAnimated:NO];
}

答案 2 :(得分:0)

帮助我的是以编程方式创建自己的布局约束

tableView.top <-> tableView.superview.top

而不是通常使用

tableView.top <-> tableView.superview.topLayoutGuide

并设置

view.edgesForExtendedLayout = .Top

我已经尝试了无数的布局和约束组合,除了这个之外什么都没有用。真烦人的错误

答案 3 :(得分:0)

今天遇到了这个问题,并通过引用结果视图控制器的safeAreaLayoutGuide

来解决。

刷新结果表的内容后,可以调用此方法:

// NOTE: this method is used to fix a known UIKit bug where search results that do not have a content
    // size that fills the view will be placed underneath the navigation bar when displayed. this hack
    // fixes this issue by resetting the contentInset based on the content size.
    private func adjustContentInsetForContentSize() {
        if collectionView.contentSize.height > view.frame.height {
            collectionView.contentInset = UIEdgeInsets.zero
        } else {
            collectionView.contentInset = UIEdgeInsets(top: view.safeAreaLayoutGuide.layoutFrame.origin.y, left: 0, bottom: 0, right: 0)
        }
    }

基本上,此问题是由于结果视图的contentSize高度小于视图的可视区域而引起的。结果呈现会在contentSize.height> view.frame.height时找到,因此此破解将强制内容插图正确遵守安全区域布局指南。

答案 4 :(得分:0)

我已经使用UISearchController多年了,但是今天是我第一次遇到这个问题 我曾经设置过edgesForExtendedLayout = []extendedLayoutIncludesOpaqueBars = true,一切都很好

但是今天不是,我需要将其反转为edgesForExtendedLayout = .all 这可能对您也有用!