本文介绍了如何使搜索栏角变圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使 searchBar
角变圆.我尝试了很多使用 layer.cornerRadius
、maskToBound = true
的方法.我正在以编程方式加载 navigationBar
中的 searchBar
.我已经尝试了许多关于堆栈溢出的解决方案,但没有任何效果.
I am trying to make searchBar
corner rounded. I have tried many ways of using layer.cornerRadius
, maskToBound = true
. I am loading the searchBar
in the navigationBar
programmatically. I have tried many solution given on stack-overflow but nothing worked.
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
}
如果有什么我能做的,请帮忙.
Please help if there is anything I can do.
推荐答案
在我当前的项目中使用以下代码.
Use following code which are working fine for me in my current project.
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;
}
}
希望对你有所帮助.
这篇关于如何使搜索栏角变圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!