我正在尝试使 searchBar
角变圆。我已经尝试了很多使用 layer.cornerRadius
的方法,maskToBound = true
。我正在以编程方式在 navigationBar
中加载 searchBar
。我尝试了很多关于堆栈溢出的解决方案,但没有任何效果。
func setupsearchbar()
{
// Setup the Search Controller
searchcontroller.searchResultsUpdater = self as? UISearchResultsUpdating
searchcontroller.hidesNavigationBarDuringPresentation = false
searchcontroller.searchBar.placeholder = "Search"
self.navigationItem.titleView = searchcontroller.searchBar
self.searchcontroller.searchBar.layer.cornerRadius = 30
self.searchcontroller.searchBar.layer.masksToBounds = true
definesPresentationContext = true
searchcontroller.searchBar.delegate = self
}
如果有什么我能做的,请帮忙。
最佳答案
在我当前的项目中使用以下对我来说工作正常的代码。
if let textfield = searchController.searchBar.value(forKey: "searchField") as? UITextField {
textfield.textColor = UIColor.blue
if let backgroundview = textfield.subviews.first {
// Background color
backgroundview.backgroundColor = UIColor.white
// Rounded corner
backgroundview.layer.cornerRadius = 10;
backgroundview.clipsToBounds = true;
}
}
希望对您有所帮助。
关于ios - 如何使搜索栏角变圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52870677/