简体   繁体   English

从 UISearchController 搜索栏 swift 中删除黑色边框

[英]remove black border from UISearchController search bar swift

I have a UISearchController in my tableView.我的 tableView 中有一个 UISearchController。 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:在视图DidLoad中:

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() :我遵循了几个类似的问题,并在viewDidLoad()的上述行之后进行了以下更改:

1) searchController.searchBar.backgroundImage = UIImage() 1) searchController.searchBar.backgroundImage = UIImage()

2) searchController.searchBar.searchBarStyle = UISearchBarStyle.Minimal 2) searchController.searchBar.searchBarStyle = UISearchBarStyle.Minimal

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

4) 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:viewDidLoad ,添加了以下几行:

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:之后在didFinishLaunchingWithOptions中的 app.delegate 文件中添加了以下代码:

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)在 app.delegate 文件上也需要这个扩展名(放在类之外)

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 如何隐藏 iOS7 UINavigationBar 1px 底线

For iOS 13 / Swift 5;适用于 iOS 13 / Swift 5; the following line makes the trick:以下行是诀窍:

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

Cheers!干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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