Max Tum
Max Tum
Sep 7, 2018 · 1 min read

I've foun a solution for iOS 11 via KVO

```

private var kvoContext: UInt8 = 1
let textFieldInsideSearchBar = self.value(forKey: “searchField”) as? UITextField

let clearButton = textFieldInsideSearchBar?.value(forKey: “clearButton”) as? UIButton

clearButton?.imageView?.addObserver(self, forKeyPath: “image”, options: [.new], context: &kvoContext)

override open func observeValue(forKeyPath keyPath: String?, of object: Any?,

change: [NSKeyValueChangeKey : Any]?,

context: UnsafeMutableRawPointer?) {

if context == &kvoContext {

colorCancelButton()

}

}

private func colorCancelButton() {

let textFieldInsideSearchBar = self.value(forKey: “searchField”) as? UITextField

guard let clearButton = textFieldInsideSearchBar?.value(forKey: “clearButton”) as? UIButton else {

return

}

if #available(iOS 11, *) {

let image = clearButton.imageView?.image?.withRenderingMode(.alwaysTemplate)

clearButton.setImage(image, for: .normal)

} else {

clearButton.setImage(clearButton.imageView?.image?.withRenderingMode(.alwaysTemplate), for: .normal)

}

clearButton.tintColor = .white

}

```

    Max Tum

    Written by

    Max Tum