如何更改SearchBar边框颜色

我想将我的Search Bar灰色边框颜色更改为白色。 现在看起来像这样:

搜索栏

我在使用这行代码后达到了这个效果,但是“内边框”仍然是灰色的:

 var searchBar: UISearchController! self.searchBar.searchBar.backgroundColor = UIColor.whiteColor() self.searchBar.searchBar.layer.borderWidth = 3 self.searchBar.searchBar.layer.borderColor = UIColor.whiteColor().CGColor self.searchBar.searchBar.layer.backgroundColor = UIColor.whiteColor().CGColor self.searchBar.searchBar.tintColor = UIColor(red: 0.3, green: 0.63, blue: 0.22, alpha: 1) 

有人能帮我吗?

下面的代码的结果。

 self.searchBar.searchBar.searchBarStyle = UISearchBarStyle.Prominent self.searchBar.searchBar.translucent = false let textFieldInsideSearchBar = self.searchBar.searchBar.valueForKey("searchField") as? UITextField textFieldInsideSearchBar?.backgroundColor = UIColor.whiteColor() self.searchBar.searchBar.barTintColor = UIColor.whiteColor() 

上面的代码会给你所有的白色search栏,但可能会在search栏的顶部和底部出现黑色的线条,你可以从附件中看到。 如果你看到黑线,如果你不想要它们,改变;

 self.searchBar.searchBar.barTintColor = UIColor.whiteColor() 

 self.searchBar.searchBar.backgroundImage = UIImage(named: "nameOfYourWhiteImage") 

你将有一个干净的白色search栏。 希望这可以解决你的问题。 祝你好运!