0

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

The result

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.

Community
  • 1
  • 1
Percolator
  • 513
  • 5
  • 25

2 Answers2

1

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
emresancaktar
  • 1,507
  • 17
  • 25
  • The soultion worked for the view seen in the picture in my original post, they are now the same color. The problem is that when I select the search bar, the navbar used for searching is invisible. Any suggestions how to solve that? – Percolator Apr 03 '16 at 11:37
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

Artem Deviatov
  • 970
  • 12
  • 20
  • `self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default) self.navigationController?.navigationBar.shadowImage = UIImage()` solved it for me – Percolator Apr 03 '16 at 11:45