Navigation Bar and Search Controller

孤街浪徒 提交于 2019-12-25 05:17:17

问题


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
 }

The search bar attributes:

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

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.


回答1:


Same color:

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

Separator:

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

But it works only if you set transluent field to false




回答2:


I think you have a Translucent in your NavBar. You should off the Transculent with this code, you can use in your viewDidLoad method.

self.navigationController?.navigationBar.translucent = false

Also you can toggle translucent in the interface builder. Select your Navigation Controller then in the Document Outline select the Navigation Bar and just change it in the Attributes Inspector Uncheck the Translucent option.

If you don't want to disappear your navigationBar when user tap the your SearchBar use this;

searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false


来源:https://stackoverflow.com/questions/36384135/navigation-bar-and-search-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!