I used UISearchBar in the project, but I can't change the view of it. I want to set border around the search bar.
Can I change UIsearchbar as the same as this image?
I used UISearchBar in the project, but I can't change the view of it. I want to set border around the search bar.
Can I change UIsearchbar as the same as this image?
You can set invisible searchbar with transparent borders and background and below that just add image/draw subview if the shape you want. I think Adding UIView with property explicitly: layer.cornerRadius = 4.0 should work :)
Yes you can definitely change the appearance of UISearchbar to the image displayed.
Basically you will need to customise the properties of UISearchBar and associated UITextField and modify the visibility of the search button.
I found "the" article which will help you to achieve the desired result.
http://en.codecloud.net/989.html
Hope this helps.
You can customise the appearance of UISearchbar as:
// Get UISearchBar UITextFiled
let txfSearchField = Searchbar.value(forKey: "_searchField") as! UITextField
// set Search Bar texfield corder radius
txfSearchField.layer.cornerRadius = 8.0
// set Search Bar texfield border colour and width
txfSearchField.layer.borderWidth = 2.0
txfSearchField.layer.borderColor = UIColor.lightGray.cgColor
// set Search Bar tintColor.
Searchbar.barTintColor = UIColor.white
// Change the search bar placeholder text color
let placeholderlabels = txfSearchField.value(forKey: "_placeholderLabel") as! UILabel
placeholderlabels.textColor = UIColor.lightGray
If you have many custom search bar in different viewControllers. I used the correct answer and made custom class for searchBar. I used it in storyboard.
import UIKit
class CustomSearchBar: UISearchBar {
func setup(){
// set Search Bar tintColor.
self.backgroundImage = UIImage()
self.barTintColor = UIColor.white
// Get UISearchBar UITextFiled
guard let textSearchField = self.value(forKey: "_searchField") as? UITextField else {
return
}
// set Search Bar texfield corder radius
textSearchField.layer.cornerRadius = 6.0
// set Search Bar texfield border colour and width
textSearchField.layer.borderColor = UIColor("#446D95").cgColor
textSearchField.layer.borderWidth = 1.0
textSearchField.layer.masksToBounds = true
}
}
Only Call setup function in viewDidLoad of the viewController that you want to use the searchBar.
override func viewDidLoad(){
searchBar.setup()
}