How to make searchbar corner round

让人想犯罪 __ 提交于 2020-01-04 04:36:51

问题


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.


回答1:


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;
    }
}

I hope this will help you.



来源:https://stackoverflow.com/questions/52870677/how-to-make-searchbar-corner-round

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!