Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchBar's set_cancelButtonText: ivar is prohibited

Tags:

swift

ios13

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.

like image 555
AaoIi Avatar asked Sep 21 '19 12:09

AaoIi


2 Answers

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

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).title = "Whatever"
like image 86
Gereon Avatar answered Nov 15 '22 12:11

Gereon


Objective-C version:

[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitle:@"CancelText"];
like image 25
steve Avatar answered Nov 15 '22 12:11

steve