曲线搜索栏 Swift 4

Curve search bar Swift 4

我在导航控制器上以编程方式更改搜索栏设计时遇到问题。我只想使搜索栏曲线如下图所示:

我已经尝试了很多stack overflow的例子,但是还是改不了形状。这是现在的样子:

这是源代码,关于已经完成的工作:

//MARK for searchBar UI
    searchController.dimsBackgroundDuringPresentation = false
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.searchBar.placeholder = " Search for programs..."
    searchController.searchBar.sizeToFit()
    searchController.searchBar.isTranslucent = false
    searchController.searchBar.backgroundImage = UIImage()
    searchController.searchBar.delegate = self
    navigationItem.titleView = searchController.searchBar
    self.searchController.searchBar.layer.cornerRadius = 20.0
    self.searchController.searchBar.clipsToBounds = true
    // SearchBar text
    let textFieldInsideUISearchBar = searchController.searchBar.value(forKey: "searchField") as? UITextField
    textFieldInsideUISearchBar?.borderStyle = .roundedRect
    textFieldInsideUISearchBar?.font = textFieldInsideUISearchBar?.font?.withSize(14)

现在,我真的不知道如何更改它,在此先感谢。

增加圆角半径应该有效

这对你有帮助,

let searchBar = UISearchBar()

viewDidLoad()

searchBar.sizeToFit()
searchBar.placeholder = "Search"
self.navigationController?.navigationBar.barTintColor = UIColor.green //this is just for reference
self.navigationItem.titleView = searchBar

if let textfield = searchBar.value(forKey: "searchField") as? UITextField {
    textfield.textColor = UIColor.blue
    if let backgroundview = textfield.subviews.first {
        backgroundview.backgroundColor = UIColor.blue
        backgroundview.layer.cornerRadius = 20;
        backgroundview.clipsToBounds = true;
    }
}

上面的代码会给你下面的输出,