UISearchBar customization code crashes in iOS7, works on iOS6

落花浮王杯 提交于 2019-12-11 10:39:23

问题


I have an iOS application, built to support iOS5.1, and I was customizing the cancel button inside search bars.

The code I'm using and that works until iOS6.1 is the following:

id barButtonAppearanceInSearchBar =
    [UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil];
[barButtonAppearanceInSearchBar setTitle:
    [self localizedStringValueFor:@"Cancel"]];

Somehow the second line will cause a bad access exception, the barButtonAppearanceInSearchBar seems to be deallocated when I try to set the value for the button's text.

localizedStringForValue is a custom method that will return a String with the translation for the provided key or the key itself if not translation exists.

Now when I run the app on iOS7' simulator the app always crashes. Been trying to figure out what I'm doing wrong but no luck so far.


回答1:


setTitle is not a support UIAppearanceContainer method. Only properties that are marked with UI_APPEARANCE_SELECTOR are supported.

You will have to set the title manually when you create the UISearchBar.



来源:https://stackoverflow.com/questions/18967364/uisearchbar-customization-code-crashes-in-ios7-works-on-ios6

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