Search code examples
iosswift2uinavigationbarios9uisearchcontroller

iOS 9 hairline between UINavigationController and UISearchBar


I have a UINavigationController under which a UISearchController is placed programmatically.

searchController.searchBar.barTintColor = UIColor(red: 242/255.0, green: 82/255.0, blue: 46/255.0, alpha: 0.4)
searchController.searchBar.translucent = false
searchController.searchBar.layer.borderColor = UIColor.whiteColor().CGColor
searchController.searchBar.layer.borderWidth = 0

I setup my UISearchController like above. I set the UINavigationBar its design globally like this:

UINavigationBar.appearance().translucent = false
UINavigationBar.appearance().barTintColor = UIColor(red: 242/255.0, green: 82/255.0, blue: 46/255.0, alpha: 1.0)
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarPosition: .Any, barMetrics: .Default)
UINavigationBar.appearance().layer.shadowRadius = 0
UINavigationBar.appearance().layer.shadowOpacity = 0
UINavigationBar.appearance().layer.shadowOffset = CGSizeMake(0,0)
UINavigationBar.appearance().titleTextAttributes = [
    NSForegroundColorAttributeName: UIColor.whiteColor(),
    NSFontAttributeName: UIFont(name: "JosefinSans-SemiBold", size: 18)!
]

It should've removed that 1px hairline border, but it didn't. I read that it's because of the shadow image, but I can't seem to remove that 1px hairline. Who can help me out?


Solution

  • Try replacing this:

    searchController.searchBar.layer.borderColor = UIColor.whiteColor().CGColor
    searchController.searchBar.layer.borderWidth = 0
    

    with this:

    searchController.searchBar.layer.borderColor = UIColor(red: 242/255.0, green: 82/255.0, blue: 46/255.0, alpha: 1).CGColor
    searchController.searchBar.layer.borderWidth = 1