I am working on Swift.
As regular SearchBar By default its appearing like Following Pic(1).
But as per our design requirement I need to change it as Following Pic(2).
I tried using the following code I got like Following Pic(3)
**When I tried the following code the right view is getting hide and I could not able to see the text field rightview
Search icon completely invisible**
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
Can anyone help me to get the requirement. I can understand if you provide even on objective C as well
Try This , and call on 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
}