标签:des style blog http io color ar os sp
思路是获取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 }
标签:des style blog http io color ar os sp
原文地址:http://www.cnblogs.com/lYcHeeM/p/4099693.html