8

Previously in iOS 12 the same code was allowed, but now when i try to run the same code on iOS 13 it crashes giving me the same error:

Terminating app due to uncaught exception 'NSGenericException', reason: 'Access to UISearchBar's set_cancelButtonText: ivar is prohibited. This is an application bug'

Which is related to this line of code:

searchController.searchBar.setValue("Cancel".localized, forKey:"_cancelButtonText")

Now i know the access of setValue is now prohibited but how is it possible to overcome this crash and change the title of cancel button, since there is no property included in the searchbar.

AaoIi
  • 8,288
  • 6
  • 45
  • 87

4 Answers4

14

Instead of messing around with the undocumented view hierarchy, you can use UIAppearance:

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).title = "Whatever"
Gereon
  • 17,258
  • 4
  • 42
  • 73
4

Objective-C version:

[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitle:@"CancelText"];
steve
  • 615
  • 6
  • 14
0

For Objective-C , You can use this if you have problem with UISearchController or change it however you're using.

[[UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[[UISearchController class]]] prefersLargeTitles];
Brtgmaden
  • 839
  • 7
  • 8
-1

UISearchDisplayController is deprecated since iOS 8.0 avoid using it

Alex2013
  • 39
  • 3