【问题标题】:Curve search bar Swift 4 [duplicate]曲线搜索栏 Swift 4 [重复]
【发布时间】:2018-06-04 04:54:42
【问题描述】:

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

我在堆栈溢出中尝试了很多示例,但我仍然未能更改形状。这是现在的样子:

这里是源代码,关于已经做过的事情:

//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)

现在,我真的不知道如何更改它,提前谢谢。

【问题讨论】:

  • 你可以试试stackoverflow.com/a/49810660/4601900和试试searchBarContainer height
  • 尝试增加searchBar.layer.cornerRadius 值
  • @PPL 抱歉,我试过了。但还是失败了
  • self.searchController.searchBar.layer.cornerRadius = self.searchController.searchBar.frame.height/2.0

标签: swift uisearchbar uinavigationitem uisearchcontroller programmatically


【解决方案1】:

这对你有帮助,

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

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

【讨论】:

  • 谢谢队友,我只是意识到只需要添加这一行` if let backgroundview = textfield.subviews.first { backgroundview.backgroundColor = UIColor.blue backgroundview.layer.cornerRadius = 20; backgroundview.clipsToBounds = true; } `
【解决方案2】:

增加圆角半径应该可以工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-10
    • 1970-01-01
    • 2017-11-22
    相关资源
    最近更新 更多