之前在自定义UISearchBar的时候碰到了钉子.(Xcode7.2/iOS 7.1)
自定义UISearchBar的时候有遇到不熟悉的属性,想要达到想要的的自定义效果.只能像往常一样求助网络.结果各种各样的都有,许多还是不起作用的,准确的说是和我的要求不匹配,当然很多是我没有弄懂造成的.(这里吐槽一下,经常因为网络不好使直接百度,结果证明还是stackoverflow靠谱.)
方法都针对iOS 7以后.
![](http://upload-images.jianshu.io/upload_images/1512613-dc93eb64f27a00d5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
- 1.0 输入字体颜色和placeholder颜色
UITextField *searchField = [_searchBar valueForKey:@"_searchField"];
searchField.textColor = TEXTCOLOR;
[searchField setValue:PLACEHOLDERCOLOR forKeyPath:@"_placeholderLabel.textColor"];
- 2.0 左边搜索图标Offset/Image/Size
在这里我直接create一个view然后赋给leftView并不起作用,望指点.
searchField.leftView.frame = CGRectMake(0, 0, 18, 18);
[_searchBar setImage:[UIImage imageNamed:@"searchIcon"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
//[_searchBar setPositionAdjustment:UIOffsetMake(200, 0) forSearchBarIcon:UISearchBarIconSearch];
- 3.0 textField Offset
[_searchBar setSearchTextPositionAdjustment:UIOffsetMake(8, 0)];
- 4.0 搜索条背景颜色&搜索输入框背景色
_searchBar.barTintColor = TINTCOLOR;
searchTextField = [[[_searchBar.subviews firstObject] subviews] lastObject];
searchTextField.backgroundColor = TINTCOLOR;
- 5.0 闪烁光标颜色
_searchBar.tintColor = [UIColor colorWithRed:55/255.0 green:200/255.0 blue:237/255.0 alpha:1.0];
- 6.0 搜索文本框背景图片&搜索框背景图片
[_searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"top"] forState:UIControlStateNormal];
[_searchBar setBackgroundImage:[UIImage imageNamed:@"top"]];
***
由于笔者知识有限,如有错误,欢迎指出。