31

我有一个带有 UISearchBar 和 UISearchDisplayController 的 UITableViewController。这存在于 UINavigationController 中的 UIViewController 中的容器视图中。我制作了这张图片来帮助描述结构:

在此处输入图像描述

这才是真正的样子:

在此处输入图像描述

当我点击搜索栏时,我必须隐藏导航栏。通常,这会自行发生,但由于我的 UITableViewController 位于容器视图中,因此我必须自己处理该更改。这就是它的样子,注意状态栏是白色的,因为导航栏是白色的,即使它现在是隐藏的。

在此处输入图像描述

一旦我开始输入一些搜索文本,结果就会显示出来。如果我向上滚动这些结果,它们会在搜索栏下方通过,但它们会与状态栏重叠,这很不吸引人。

在此处输入图像描述

如果不涉及 Container View,那么这一切都按预期工作,并且表格内容在状态栏下方传递,但是在涉及 ContainerView 的情况下,表格文本和状态栏会发生冲突。

如何让文本像往常一样在状态栏下移动?

4

8 回答 8

37

我已经搜索了几个小时,我的最终结果是将这一行放在 viewDidLoad 中: self.extendedLayoutIncludesOpaqueBars = YES;

问题解决了 :)

于 2015-07-10T13:32:48.507 回答
5

尝试设置你definesPresentationContextviewDidLoadTableViewController

迅速

override func viewDidLoad() {
    super.viewDidLoad()

    definesPresentationContext = true
}

Objective-C

- (void)viewDidLoad {
    [super viewDidLoad];

    self.definesPresentationContext = YES;
}
于 2014-11-07T17:46:14.583 回答
4

这对我有用:

做:

  • 使用 UISearchController(不是单独放置的 UISearchBar)
  • 如果还没有,请将您的 VC 放在 UINavigationController 中。如果需要,将导航设置为不“显示导航栏”。
  • 对 UITableView 使用自动布局(不是弹簧和支柱)并将表格顶部固定到 VC 视图的顶部。
  • 添加此委托方法:

- (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar { return UIBarPositionTopAttached; }

不:

  • 摆弄edgeForExtendedLayout
  • 摆弄extendedLayoutIncludesOpaqueBars
  • 摆弄表格的 contentInset
于 2016-12-26T23:20:21.050 回答
3

基本上这是由于导航栏的透明性,通常视图控制器通过更正拥有的视图或子视图的顶部插入(如果它们是(或继承)自 UIScrollView)来修复重叠。您有 2 个选项,一个是将导航栏的透明度设置为 no,另一个是将导航栏的透明度设置edgeForExtendedLayout为 none,只留下底部。

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

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

这些建议仅适用于 iOS7,如果您在设置这些属性之前部署在较低的目标检查。
另一种方法,但我没有测试可以读取--topLayoutGuide长度并在 -searchDisplayControllerWillBeginSearch尝试设置相同长度的 topInsets。这样,您仍然应该保持半透明。

于 2014-04-17T05:17:14.523 回答
3

我有 UISearchBar 和 UISearchDisplayController。

在 viewdidload 中:

self.edgesForExtendedLayout = UIRectEdgeNone;
[searchDisplayController.searchBar setBackgroundImage:[self imageWithColor:ETSBaseColor] forBarPosition:0 barMetrics:UIBarMetricsDefault];

从 UIColor 获取图像的方法:

- (UIImage *)imageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}
于 2015-09-03T19:06:37.133 回答
0

我有同样的问题:

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
    controller.searchBar.searchBarStyle = UISearchBarStyleDefault; // Used to cover UIStatusBar
}

- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
{
    controller.searchBar.searchBarStyle = UISearchBarStyleMinimal; // Used not to show top and bottom separator lines
}
于 2014-08-12T14:13:32.873 回答
0

以下黑客对我有用:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return (self.searchController.isActive  && section == 0) ? 22.0f : 0.0f;
}
于 2017-05-24T10:19:50.830 回答
0

在我的情况下,我不想隐藏 UINavigationBar,但我在间隙和其他副作用方面遇到了类似的问题。其中之一是在 UIViewController 之间切换后 UISearchBar 丢失,而 UISearchDisplayController 可见(我正在使用 SWRevealViewController 在 UIViewController 之间切换)。此问题仅发生在 iPad 上。结果发现 UISearchBar 突然隐藏在 UINavigationBar 后面了。现在我用 UITableViewController 中的以下代码行解决了我所有的问题,这些代码出现在 UIContainerView 中:

- (UINavigationController *)navigationController {
    return nil;
}

这些行阻止 UISearchDisplayController 到达并更改我的 UINavigationController。我还将这个方法子类化为“MyContainerTableViewController”类,现在将这个类用于所有嵌入式 UITableViewController。

我仍在使用 UISearchDisplayController 来支持 iOS 7。

于 2016-01-13T13:35:17.763 回答