【问题标题】:UISearchController doesn't work properly with a non-translucent UINavigationBarUISearchController 不能与非半透明 UINavigationBar 一起正常工作
【发布时间】:2014-10-14 19:56:10
【问题描述】:

目前我正在尝试将 UISearchController 嵌入到我的应用程序中。但是,如果 UINavigationBar 是非半透明的,作为 UISearchController 的属性的 UISearchBar 将无法正确显示。通常在点击 UISearchBar 属性后,UINavigationBar 会向上移动以为 UISearchBar 腾出空间。您可以在以下屏幕截图中看到结果:

https://www.dropbox.com/s/172k63zr2bhj84t/Normal_behaviour.png?dl=0

但如果 UINavigationBar 的“translucent”属性设置为“NO”,则 UISearchBar 无法正常显示,因为状态栏的背景保持透明,如下图所示:

https://www.dropbox.com/s/v5cnxoj9ms6976r/Wrong_behaviour.png?dl=0

为了演示这种奇怪的行为,我修改了 Apple 提供的示例项目:

https://developer.apple.com/library/ios/samplecode/TableSearch_UISearchController/Introduction/Intro.html

在这里你可以下载修改后的版本:

https://www.dropbox.com/s/7icfe6kap98g1e8/TableSearchwithUISearchControllerObj-CandSwift_MODIFIED.zip?dl=0

修改在文件“APLMainTableViewController.m”第33行。

【问题讨论】:

  • 是的,我也看到了。非常令人沮丧。
  • 截图不存在。你能再上传一次吗?

标签: ios cocoa-touch uikit uinavigationbar uisearchcontroller


【解决方案1】:

这显然是一个错误 (rdar://20942583)。

我的解决方法是设置

self.edgesForExtendedLayout = UIRectEdgeAll;
self.extendedLayoutIncludesOpaqueBars = YES;

这允许您保持导航栏不透明。缺点是即使看不到,内容也会在栏下方流动,从而产生一些开销。

【讨论】:

  • edgesForExtendedLayout 调整对我来说不是必需的。但这行得通。
  • 这对我也有用。在过去的两天里,我一直在努力解决这个问题。然而,它确实需要这两行。
  • 目标vc上需要这两行?
  • @Godfather 不,在展示的 VC 上。
  • 为什么是苹果为什么!你用这个偷走了我三个小时的生命,我想要他们回来!
【解决方案2】:

我只需要:

func viewDidLoad() { 

    extendedLayoutIncludesOpaqueBars = true
}

【讨论】:

    【解决方案3】:

    一种解决方法是在搜索即将变为活动之前使状态栏半透明,并在搜索即将变为时移除半透明>不活动

    您可以通过将视图控制器注册为UISearchController 的委托并实现willPresentSearchControllerwillDismissSearchController 方法来实现此目的。例如(在Swift):

    将您的视图控制器声明为UISearchController 的代表:

     class MyViewController: UITableViewController, UISearchControllerDelegate
    

    不要忘记将其实际设置为委托,例如在viewDidLoad 中添加:

        searchController.delegate = self
    

    最后:

    func willPresentSearchController(searchController: UISearchController) {
        navigationController?.navigationBar.translucent = true
    }
    
    func willDismissSearchController(searchController: UISearchController) {
        navigationController?.navigationBar.translucent = false
    }
    

    【讨论】:

    • 这对我有用。为什么 Apple 甚至支持更改导航栏上的 barTintColor?
    • 头疼了几天。谢谢你,救了我的命。推送新视图控制器时需要将其设置回 false 并在 viewDidAppear 上将其设置为 searchController.isActive。
    【解决方案4】:

    好的,这个调试起来非常痛苦,但修复起来还不错。这完全取决于 Apple 改变导航栏外观的方式。可以通过创建UINavigationBarAppearance 对象来修复它,使用您想要的视觉属性(即背景颜色等)对其进行配置,然后将其分配给UINavigationBar.appearance() 上的standardAppearancescrollEdgeAppearance - 你可以有两个不同的实例如果需要,可以进行不同的设置。

    一个简单的实现可能如下所示:

    let appearance = UINavigationBarAppearance()
    appearance.configureWithDefaultBackground()
    appearance.backgroundColor = barColor
    appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: textColor]
    
    UINavigationBar.appearance().standardAppearance = appearance
    UINavigationBar.appearance().scrollEdgeAppearance = appearance
    

    (自然地将 barColor 和 textColor 替换为您选择的颜色!)

    【讨论】:

      【解决方案5】:

      如果有人遇到像非半透明隐藏搜索栏这样的问题,你可以这样:

      self.definesPresentationContext = true

      问候

      【讨论】:

        猜你喜欢
        • 2014-11-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-10
        • 2014-07-01
        • 1970-01-01
        • 2016-07-19
        • 1970-01-01
        相关资源
        最近更新 更多