当有需求要修改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.youkuaiyun.com/jacob_ios/article/details/51347595
本文介绍了如何根据不同iOS版本调整searchBar的背景色和文本框颜色。对于iOS 10及以下版本,通过修改searchBar的backgroundImage实现;对于iOS 10以上版本,则通过对searchController的背景进行操作来完成。

211

被折叠的 条评论
为什么被折叠?



