iOS10中UISearchController对searchBar的修改

当有需求要修改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];
}

demo效果图

关于searchBar的使用:
http://blog.csdn.net/jacob_ios/article/details/51347595

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值