简体   繁体   中英

remove black border from UISearchController search bar swift

I have a UISearchController in my tableView. Also note that there is a navigation item in the top. My issue is that i have a black border in the top and bottom when I load the page, however, it is not present when I click the search bar.

Search bar on page load (with black border):

在此处输入图片说明

After clicking on search bar (no black border):

在此处输入图片说明

Here is the code related:

let searchController = UISearchController(searchResultsController: nil)

in viewDidLoad:

searchController.searchBar.barTintColor = UIColor.redColor()
        searchController.searchBar.tintColor = UIColor.whiteColor()

I followed several similar questions and made the following changes after the above lines in viewDidLoad() :

1) searchController.searchBar.backgroundImage = UIImage()

2) searchController.searchBar.searchBarStyle = UISearchBarStyle.Minimal

3) searchController.searchBar.layer.borderColor = UIColor.clearColor().CGColor

4)

 searchBar.layer.borderWidth = 1
    searchBar.layer.borderColor = UIColor.whiteColor().CGColor

None worked. Is this an issue with the order I am using the code, or how would I get rid of these lines?

Fixed this after a lot of search. Here's how i did it. In viewDidLoad , added the following lines:

self.searchController.searchBar.translucent = false
    self.searchController.searchBar.backgroundImage = UIImage()
    self.searchController.searchBar.barTintColor = UIColor.redColor()
    self.searchController.searchBar.tintColor = UIColor.whiteColor()

After that in app.delegate file in didFinishLaunchingWithOptions added the following code:

let backgroundColor = UIColor.redColor()
        let foregroundColor = UIColor.whiteColor()


        UIApplication.sharedApplication().statusBarStyle = .LightContent

        UINavigationBar.appearance().shadowImage = UIImage()

        UINavigationBar.appearance().setBackgroundImage(backgroundColor.toImage(), forBarMetrics: UIBarMetrics.Default)

        UINavigationBar.appearance().tintColor = foregroundColor

        UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: foregroundColor]

Also need this extension on the app.delegate file (place outside of the class)

extension UIColor{
    func toImage() -> UIImage {
        let rect = CGRectMake(0, 0, 1, 1)
        UIGraphicsBeginImageContextWithOptions(rect.size, true, 0)
        self.setFill()
        UIRectFill(rect)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
}

(Thanks to Noel for the extension taken from this answer )

And finally, the desired result:

在此处输入图片说明

I think it is the navigation bar you need to remove the border for.

How to hide iOS7 UINavigationBar 1px bottom line

For iOS 13 / Swift 5; the following line makes the trick:

  UISearchBar.setBackgroundImage(UIImage(), for: .any, barMetrics: .default)

Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM