UIAppearance - 工具栏和搜索栏崩溃



通过UIAppearance自定义UIToolbar和UISearchBar方面时,收到无法识别的选择器发送到实例错误。

奇怪的是,它只在6.1或更低版本上崩溃,在iOS7上很好,它没有崩溃。

这是我正在使用的代码:

[[UIToolbar appearance] setBackgroundImage:[UIImage imageNamed:@"toolbarBackground"] forToolbarPosition:UIBarPositionBottom barMetrics:UIBarMetricsDefaultPrompt];
[[UIToolbar appearance] setTintColor:[UIColor whiteColor]];
[[UISearchBar appearance]setBackgroundImage:[UIImage imageNamed:@"searchBarBackground"]  forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
[[UISearchBar appearance] setTintColor:[UIColor whiteColor]];

应该没事。但每次我在iOS 6.1模拟器上启动应用程序时,我都会得到

-[_UIAppearance setBackgroundImage:forBarPosition:barMetrics:]: unrecognized selector sent to instance 0xaba4550

用于UIToolbar和UISearchBar。我确信是他们造成了崩溃,因为如果我评论这些行,应用程序就会正常启动。

这个代码出了什么问题?我真的受够了。

编辑我通过在需要定制的类中设置方面来实现它,比如:

[[UISearchBar appearance]setBackgroundImage:[UIImage imageNamed:@"searchBarBackground"]];

但现在,当我点击搜索栏时,它会给我默认的方面。

奇怪的是,它只在6.1或更低版本上崩溃,在iOS7上很好,它没有崩溃。

根据文档,UISearchBar上的setBackgroundImage:forBarPosition:barMetrics:仅在iOS 7.0及更高版本中可用。

这就是为什么在iOS 6.1上出现无法识别的选择器异常的原因。

我设法使它以这种方式工作:

#define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

UIImage *toolbarImage = [UIImage imageNamed:@"toolbarBackground"];
[self.navigationController.toolbar setBackgroundImage:toolbarImage forToolbarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
UIImage *searchBarImage = [UIImage imageNamed:@"searchBarBackground"];
if (SYSTEM_VERSION_LESS_THAN(@"7.0"))
     [self.searchDisplayController.searchBar setBackgroundImage:searchBarImage];
 else
     [self.searchDisplayController.searchBar setBackgroundImage:searchBarImage forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];

在需要自定义的类中。

最新更新