更改titlecolor of cancel button
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],
UITextAttributeTextColor,
[UIColor whiteColor],
UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)],
UITextAttributeTextShadowOffset,
nil]
forState:UIControlStateNormal];
更改bgColor of cancel button,use in searchBarShouldBeginEditing
if ([[[UIDevice currentDevice]systemVersion]floatValue] >=7) {
UIButton *cancelButton;
UIView *topView = searchBar.subviews[0];
for (UIView *subView in topView.subviews) {
if ([subView isKindOfClass:NSClassFromString(@"UINavigationButton")]) {
cancelButton = (UIButton*)subView;
}
}
if (cancelButton) {
//Set the new title of the cancel button
[cancelButton setTitle:@"搜索" forState:UIControlStateNormal];
[cancelButton setBackgroundColor:[UIColor orangeColor]];
}
} else{
UIButton *cancelButton = nil;
for (UIView *subView in self.searchDisplayController.searchBar.subviews) {
if ([subView isKindOfClass:[UIButton class]]) {
cancelButton = (UIButton*)subView;
}
}
if (cancelButton){
//Set the new title of the cancel button
[cancelButton setTitle:@"搜索" forState:UIControlStateNormal];
[cancelButton setBackgroundColor:[UIColor orangeColor]];
}
}
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],
UITextAttributeTextColor,
[UIColor whiteColor],
UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)],
UITextAttributeTextShadowOffset,
nil]
forState:UIControlStateNormal];
更改bgColor of cancel button,use in searchBarShouldBeginEditing
if ([[[UIDevice currentDevice]systemVersion]floatValue] >=7) {
UIButton *cancelButton;
UIView *topView = searchBar.subviews[0];
for (UIView *subView in topView.subviews) {
if ([subView isKindOfClass:NSClassFromString(@"UINavigationButton")]) {
cancelButton = (UIButton*)subView;
}
}
if (cancelButton) {
//Set the new title of the cancel button
[cancelButton setTitle:@"搜索" forState:UIControlStateNormal];
[cancelButton setBackgroundColor:[UIColor orangeColor]];
}
} else{
UIButton *cancelButton = nil;
for (UIView *subView in self.searchDisplayController.searchBar.subviews) {
if ([subView isKindOfClass:[UIButton class]]) {
cancelButton = (UIButton*)subView;
}
}
if (cancelButton){
//Set the new title of the cancel button
[cancelButton setTitle:@"搜索" forState:UIControlStateNormal];
[cancelButton setBackgroundColor:[UIColor orangeColor]];
}
}