Useful UISearchBar Extensions
- If you want to access the TextField inside the UISearchBar and want to set the properties
123456789extension UISearchBar{public var textField: UITextField? {let subViews = subviews.flatMap { $0.subviews }guard let textField = (subViews.filter { $0 is UITextField }).first as? UITextField else {return nil}return textField}} - If you want to remove the whitespaces and newline character from UISearchBar
12345 extension UISearchBar{public var trimmedText: String? {return text?.trimmingCharacters(in: .whitespacesAndNewlines)}}
- If you want to clear text of UISearchBar, use this method
12345 extension UISearchBar{public func clear() {text = ""}}