当有需求要修改searchBar的背景颜色或者文本框颜色时,ios9的代码已不能修改。记录。
根据版本的不同做不同的处理:
1、在ios10一下,获得searchBarTextField是lastObject,ios10是第二个
2、修改searchBar的背景色,10以下是,直接修改searchBar的backgroundImage;10以上,直接获得searchBackground这个视图,然后设置alpha为0,在对searchController的背景进行修改。
- (void)willPresentSearchController:(UISearchController *)searchController {
UIView *searchBarTextField = nil;
if ([[UIDevice currentDevice].systemVersion floatValue] > 10.0) {
UIView *searchBackground = [[searchController.searchBar.subviews.firstObject subviews] objectAtIndex:0];
searchBackground.alpha = 0;
searchController.view.backgroundColor = [UIColor redColor];
searchBarTextField = [[searchController.searchBar.subviews.firstObject subviews] objectAtIndex:1];
} else {
searchController.searchBar.backgroundImage = [UIImage imageWithColor: [UIColor whiteColor]];
searchBarTextField = [[searchController.searchBar.subviews.firstObject subviews] lastObject];
}
searchBarTextField.backgroundColor = [UIColor redColor];
}
关于searchBar的使用:
http://blog.csdn.net/jacob_ios/article/details/51347595