Created
April 28, 2019 13:05
-
-
Save alansvits/d7158d1db7fe1ae9fc809bc5622a73cf to your computer and use it in GitHub Desktop.
setTextFieldColor of searchBar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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