搜索控制器中的搜索栏重叠状态栏 [英] Search bar overlapping status bar in search controller

查看:33
本文介绍了搜索控制器中的搜索栏重叠状态栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UITableViewController 中以编程方式创建了一个 UISearchController.它工作正常,但搜索栏未与状态栏一起正确显示.这是我的代码和一些屏幕截图.取消搜索时也会制作有趣的动画.

I created a UISearchController programmatically in a UITableViewController. It works fine but the search bar isn't displaying correctly with the status bar. Here is my code and some screenshots. It also makes a funny animation when canceling the search.

- (void)viewDidLoad
{
    [super viewDidLoad];

    _resultsTableViewController = [ResultsTableViewController new];
    _searchController = [[UISearchController alloc] initWithSearchResultsController:_resultsTableViewController];
    _searchController.searchResultsUpdater = _resultsTableViewController;
    _searchController.dimsBackgroundDuringPresentation = NO;
    self.definesPresentationContext = YES;
    self.tableView.tableHeaderView = _searchController.searchBar;       
}

状态栏应该有更多的填充.

There should be more padding here with the status bar.

当你取消搜索时,我在这里看到一个糟糕的动画,那就是状态栏的高度.

When you I cancel searching I get a bad animation here that's the height of the status bar.

推荐答案

从你的截图看来你正在使用 iOS 11,在这个版本中 UISearchController 搜索栏被添加到 UI 的方式已经改变.在 iOS 11 上,导航项负责显示搜索,因此 UIKit 尚未更新以正确处理表标题视图中显示的搜索栏.

From your screenshots it appears you're working on iOS 11, with this version the way the UISearchController search bar is added to UI has changed. On iOS 11 is the navigation item that takes care of displaying search so UIKit has not been updated to correctly handle the search bar presented in the table header view.

在 iOS ≤10 上你应该继续使用

On iOS ≤10 you should continue to use

self.tableView.tableHeaderView = _searchController.searchBar;

但切换到

self.navigationItem.searchController = _searchController;
self.navigationItem.hidesSearchBarWhenScrolling = YES;

在 iOS 11 及更高版本上.

on iOS 11 and later.

这篇关于搜索控制器中的搜索栏重叠状态栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆