UISearchBar's set_cancelButtonText: ivar is prohibited

南笙酒味 提交于 2019-12-09 18:32:03

问题


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.


回答1:


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

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).title = "Whatever"



回答2:


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];



回答3:


Objective-C version:

[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitle:@"CancelText"];



回答4:


UISearchDisplayController is deprecated since iOS 8.0 avoid using it



来源:https://stackoverflow.com/questions/58040519/uisearchbars-set-cancelbuttontext-ivar-is-prohibited

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!