I don't think there's an easy way to hook into the X button.
However, you can hook into its direct consequence: cleared text.
Try this:
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
if searchText == "" {
print("UISearchBar.text cleared!")
}
}
The side effect is this also gets called when you manually clear your text.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…