导航栏和搜索控制器

Navigation Bar and Search Controller

我希望我的导航栏和搜索栏颜色相同。我也想摆脱它们之间的细线,但与第一个相比,这似乎是一个小问题。 导航栏属性是这样设置的:

self.navigationController?.navigationBar.barTintColor = ColorHelper.sharedInstance.LightPink()
    if let navBarFont = UIFont(name: "HelveticaNeue-Light", size: 25.0) {
        let navBarAttributesDictionary: [String: AnyObject]? = [
            NSForegroundColorAttributeName: UIColor.whiteColor(),
            NSFontAttributeName: navBarFont
        ]
        self.navigationController?.navigationBar.titleTextAttributes = navBarAttributesDictionary
 }

搜索栏属性:

searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
searchController.searchBar.placeholder = "Search for new friends"
tableView.tableHeaderView = searchController.searchBar
searchController.searchBar.backgroundColor = ColorHelper.sharedInstance.LightPink()
searchController.searchBar.barTintColor = ColorHelper.sharedInstance.LightPink()
searchController.searchBar.backgroundImage = UIImage()

这似乎是我的 ColorHelper returns 与 LightPink 不同的值,但事实并非如此。我已经检查了颜色的十六进制值,导航栏显示的颜色不正确,比实际颜色浅了一点。任何想法为什么?改变 .barStyle 并没有改变任何东西。

我认为您的 NavBar 中有一个 Translucent。你应该用这段代码关闭 Transculent,你可以在你的 viewDidLoad 方法中使用。

self.navigationController?.navigationBar.translucent = false

您还可以在界面生成器中切换半透明。 Select 您的导航控制器,然后在文档大纲 select 导航栏中,只需在 属性检查器 中更改它,取消选中半透明选项。

如果您不想在用户点击您的 SearchBar 时消失您的导航栏,请使用此选项;

searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false

同色:

我有同样的问题,我通过为我的 UISearchBar 设置 backroundImage 解决了这个问题(1x1 像素图像与我的 UINavigationBar 颜色相同)。并注意 transluent 字段 - 它必须与您的 UINavigationBar

具有相同的值

分隔符:

要删除导航栏和搜索栏之间的分隔符,您可以在 AppDelegate

中使用此代码
UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarMetrics: .Default)
UINavigationBar.appearance().shadowImage = UIImage()

但仅当您将 transluent 字段设置为 false

时它才有效