我正在开发Swift。
作为常规SearchBar,默认情况下,其外观类似于Follow Pic(1)。
但是根据我们的设计要求,我需要将其更改为Follow Pic(2)。
我尝试使用下面的代码,就像跟随Pic(3)
**当我尝试以下代码时,正确的 View 被隐藏起来,而我无法看到文本字段rightview
搜索图标完全不可见**
let searchField = (searchBar.valueForKey("_searchField") as! UITextField)
searchField.layer.cornerRadius = 15;
let searchIcon = searchField.leftView!
if (searchIcon is UIImageView) {
print("yes")
}
searchField.rightView = searchIcon
searchField.leftViewMode = .Never
searchField.rightViewMode = .Always
谁能帮我达到要求。我能理解您是否也提供 objective-c
最佳答案
试试看,然后调用viewDidLayoutSubviews()
func SearchText(to searchBar: UISearchBar, placeHolderText: String) {
searchBar.barTintColor = UIColor.clear
searchBar.backgroundColor = UIColor.clear
searchBar.isTranslucent = true
searchBar.setBackgroundImage(UIImage(), for: .any, barMetrics: .default)
let searchTextField:UITextField = searchBar.value(forKey: "searchField") as? UITextField ?? UITextField()
searchTextField.layer.cornerRadius = 15
searchTextField.textAlignment = NSTextAlignment.left
let image:UIImage = UIImage(named: "search")!
let imageView:UIImageView = UIImageView.init(image: image)
searchTextField.leftView = nil
searchTextField.font = UIFont.textFieldText
searchTextField.attributedPlaceholder = NSAttributedString(string: placeHolderText,attributes: [NSAttributedString.Key.foregroundColor: UIColor.yourCOlur])
searchTextField.rightView = imageView
searchTextField.backgroundColor = UIColor.yourCOlur
searchTextField.rightViewMode = UITextField.ViewMode.always
}
关于ios - 如何快速设置右侧的UiSearchBar搜索图标,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40240706/