Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从UISearchController搜索栏中删除黑色边框_Ios_Swift - Fatal编程技术网

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

Ios 从UISearchController搜索栏中删除黑色边框,ios,swift,Ios,Swift,我的tableView中有一个UISearchController。还请注意,顶部有一个导航项。 我的问题是,当我加载页面时,顶部和底部都有一个黑色边框,但是,当我单击搜索栏时,它不存在 页面加载时的搜索栏(带黑色边框): 单击搜索栏(无黑色边框)后: 以下是相关代码: let searchController = UISearchController(searchResultsController: nil) 在viewDidLoad中: searchController.searchB

我的tableView中有一个UISearchController。还请注意,顶部有一个导航项。 我的问题是,当我加载页面时,顶部和底部都有一个黑色边框,但是,当我单击搜索栏时,它不存在

页面加载时的搜索栏(带黑色边框):

单击搜索栏(无黑色边框)后:

以下是相关代码:

let searchController = UISearchController(searchResultsController: nil)
在viewDidLoad中:

searchController.searchBar.barTintColor = UIColor.redColor()
        searchController.searchBar.tintColor = UIColor.whiteColor()
我遵循了几个类似的问题,并在
viewDidLoad()
中的上述行之后做了以下更改:

1)
searchController.searchBar.backgroundImage=UIImage()

2)
searchController.searchBar.searchBarStyle=UISearchBarStyle.Minimal

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

(四)


没有一个奏效。这是否与我使用代码的顺序有关,或者我如何删除这些行?

我认为您需要删除边框的导航栏


经过大量搜索后修复了此问题。我是这样做的。在
viewDidLoad
中,添加了以下行:

self.searchController.searchBar.translucent = false
    self.searchController.searchBar.backgroundImage = UIImage()
    self.searchController.searchBar.barTintColor = UIColor.redColor()
    self.searchController.searchBar.tintColor = UIColor.whiteColor()
之后,
didfishlaunchwithoptions
中的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]
在app.delegate文件上也需要此扩展名(放在类之外)

(感谢Noel从中获得的延期)

最后,期望的结果是:


适用于iOS 13/Swift 5;下面这句话就是诀窍:

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

你的链接有这样一个答案:可能是潜在的有用,尽管我无法测试它。
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
    }
}
  UISearchBar.setBackgroundImage(UIImage(), for: .any, barMetrics: .default)