Skip to content

Instantly share code, notes, and snippets.

@alansvits
Created April 28, 2019 13:05
Show Gist options
  • Save alansvits/d7158d1db7fe1ae9fc809bc5622a73cf to your computer and use it in GitHub Desktop.
Save alansvits/d7158d1db7fe1ae9fc809bc5622a73cf to your computer and use it in GitHub Desktop.
setTextFieldColor of searchBar
import UIKit
extension UISearchBar {
private func getViewElement<T>(type: T.Type) -> T? {
let svs = subviews.flatMap { $0.subviews }
guard let element = (svs.filter { $0 is T }).first as? T else { return nil }
return element
}
func setTextFieldColor(color: UIColor) {
if let textField = getViewElement(type: UITextField.self) {
switch searchBarStyle {
case .minimal:
textField.layer.backgroundColor = color.cgColor
textField.layer.cornerRadius = 6
case .prominent, .default:
textField.backgroundColor = color
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment