如何在不更改文本字段光标的情况下更改 UISearchController 搜索栏中取消按钮的颜色?

How to change color of cancel button in a UISearchController search bar without changing textfield cursor?

我正在尝试更改 UISearchController 搜索栏中取消按钮的文本颜色。搜索栏的背景颜色为黑色,我希望取消按钮的文字为白色。我可以通过设置来做到这一点:

searchController.searchBar.tintColor = [UIColor whiteColor];

但是,这也会将文本字段光标的颜色更改为白色,这不是我想要的,因为这实际上使光标不可见。

以前 iOS 版本中的解决方案似乎围绕循环搜索栏的子视图并查找作为 UIButton class 的子视图。 UISearchController 中的搜索栏似乎不包含带有 UIButton class 的视图,因此这不再有效。关于如何解决这个问题的任何想法?

试试这个代码。这只会更改“取消”按钮的颜色,光标颜色将保持默认的蓝色。

[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor whiteColor]];