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

Pie*_*ero 14 objective-c uinavigationbar uitableview ios 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;
Run Code Online (Sandbox Code Playgroud)

搜索栏完美地显示在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];
}
Run Code Online (Sandbox Code Playgroud)

当我回到UITableView这样做时:

[self.navigationController popViewControllerAnimated:YES];
Run Code Online (Sandbox Code Playgroud)

该UITableView是下UINavigationBar,我怎么能解决这个问题?

谢谢

小智 10

我有完全相同的问题,设置extendedLayoutIncludesOpaqueBars为YES/ true在视图控制器,提供搜索控制器,似乎解决它.

self.extendedLayoutIncludesOpaqueBars = YES
Run Code Online (Sandbox Code Playgroud)

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


Oz *_*mon 10

这是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];
}
Run Code Online (Sandbox Code Playgroud)

  • 其中一个有效,但让你感到难过. (5认同)