导航栏和搜索控制器 [英] Navigation Bar and Search Controller

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

问题描述

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

I want my navbar and search bar to be the same color. I also want to get rid of the hairline between them but that seems a minor issue compared to the first one. The navbar attributes are set this way:

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()

似乎我的ColorHelperLightPink返回了不同的值,但没有.我检查了颜色的十六进制值,并且导航栏显示的颜色不正确,比实际颜色要浅一些.有什么想法吗?更改.barStyle不会更改任何内容.

It may seem as my ColorHelper returns different values for LightPink but it doesn´t. I've checked the color HEX-values and it's the navbar that is showing the color improperly, a bit lighter than it actually is. Any ideas why? Altering .barStyle did not change anything.

推荐答案

相同颜色:

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

I had the same problem and I solved it by setting backroundImage for my UISearchBar (1x1 pixel image with the same color as my UINavigationBar). And pay attention to transluent field – it must have the same value as your UINavigationBar

分隔符:

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

To remove separator between navigation and search bar you can use this code in your AppDelegate

UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarMetrics: .Default)
UINavigationBar.appearance().shadowImage = UIImage()

但是只有将transluent字段设置为false时,它才有效

But it works only if you set transluent field to false

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

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