UISearchBar--改变内部输入框的背景颜色
2016-01-11 16:14
585 查看
思路是获取UISearchBar的子视图,判断他是否是输入框(注意不要先入为主地认为是UITextField),最后修改背景色。至于UISearchBar的子视图结构,在不同的iOS版本可能会不一样,可通过递归地调用description方法获知,具体可见链接http://blog.csdn.net/forestml2008/article/details/32914915
下面附上代码:
下面附上代码:
1 - (void)setSearchTextFieldBackgroundColor:(UIColor *)backgroundColor 2 { 3 UIView *searchTextField = nil; 4 if (IsiOS7OrLater) { 5 // 经测试, 需要设置barTintColor后, 才能拿到UISearchBarTextField对象 6 self.barTintColor = [UIColor whiteColor]; 7 searchTextField = [[[self.subviews firstObject] subviews] lastObject]; 8 } else { // iOS6以下版本searchBar内部子视图的结构不一样 9 for (UIView *subView in self.subviews) { 10 if ([subView isKindOfClass:NSClassFromString(@"UISearchBarTextField")]) { 11 searchTextField = subView; 12 } 13 } 14 } 15 16 searchTextField.backgroundColor = backgroundColor; 17 }
相关文章推荐
- UITableViewCell的选中时的颜色设置
- Arduino - 休眠模式
- UICollectionView简单使用
- UICollectionView之定制UICollectionViewLayout
- Quartz 2D的坐标系和UIKit的坐标系对比以及相互转换
- 4.7.1. Default Argument Values(默认参数)
- UICollectionView详解
- UICollectionView高级使用示例之CircleLayout
- 实现UILabel高度和宽度的自适应和UITextView的高度自适应
- Failed to run the WC DB work queue associated with解决办法
- iOS UILabel 省略号 不变色 问题处理
- iOS UITableView去掉多余表格线,tableView去掉表头空白、改变导航栏背景色和标题属性
- 一些 UIView 中管理 Subview 常用的方法
- 一个处理程序重复的UIView animateWithDuration
- 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- 一个处理程序重复的UIView animateWithDuration?
- Android开发笔记(四十四)动态UI事件
- unixODBC without the GUI
- 动画 UIView animateWithDuration 使用详解
- Fail to decode request due to: RpcInvocation