2013-08-31 49 views
1

我得到無法識別的選擇器發送到實例錯誤,同時通過UIAppearance自定義UIToolbar和UISearchBar方面。UIAppearance - 工具欄和搜索條崩潰

奇怪的是,只有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"]]; 

但是現在,當我在搜索欄點擊,它給了我默認方面。

回答

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

在需要定製的類。

1

奇怪的是,只有6.1或更低的崩潰,在iOS7是好的,它不會崩潰。

setBackgroundImage:forBarPosition:barMetrics:UISearchBar僅在iOS版7.0及更高版本爲每the documentation

這就是爲什麼你會在iOS 6.1上遇到無法識別的選擇器異常。