swift SearchBar用户界面

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift SearchBar用户界面相关的知识,希望对你有一定的参考价值。

        
        searchBar.setPlaceholderTextColor(to:UIColor.searchBarText)
        searchBar.setTextColor(to: UIColor.white)
        searchBar.placeholder = "Search                                                                                 this text never show"
//        let searchField = searchBar.value(forKey: "_searchField") as? UITextField

        setImage(#imageLiteral(resourceName: "searchBold"), for: .search, state: .normal)
        setImage(#imageLiteral(resourceName: "closeBold"), for: .clear, state: .normal)
        UISearchBar.appearance().setImage(UIImage(), for: UISearchBarIcon.search, state: [])
        
        UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).font = UIFont.systemFont(ofSize: 18, weight: .light)
        UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = UIColor.clear
        UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).leftView = nil

extension UISearchBar
{
  var textField:UITextField {
    guard let txtField = self.value(forKey: "_searchField") as? UITextField else {
      assertionFailure()
      return UITextField()
    }
    return txtField
  }
  var clearButton:UIButton {
    guard let clearButton = textField.value(forKey: "_clearButton") as? UIButton else {
      assertionFailure()
      return UIButton()
    }
    return clearButton
  }
    func setPlaceholderTextColor(to color: UIColor)
    {
        let textFieldInsideSearchBar = self.value(forKey: "searchField") as? UITextField
        let textFieldInsideSearchBarLabel = textFieldInsideSearchBar!.value(forKey: "placeholderLabel") as? UILabel
        textFieldInsideSearchBarLabel?.textColor = color
    }
    
    func setTextColor(to color: UIColor)
    {
        let textFieldInsideSearchBar = self.value(forKey: "searchField") as? UITextField
        textFieldInsideSearchBar?.textColor = color
    }
    
    removeTopLine() {
        backgroundImage = UIImage()
    }
}

以上是关于swift SearchBar用户界面的主要内容,如果未能解决你的问题,请参考以下文章

Swift:searchBar - 如何更改“取消”按钮标题

SearchBar 和 NavigationBar 之间的空格 - Swift

iOS Swift - 滚动时如何向上移动搜索栏?

Swift 2.0 - UISearchBar 取消按钮不删除 searchBar 内容

使用Swift查找用户是不是已经显示在TableView中[重复]

text Throttling Searchbar ios swift