在使用那个UISearchBar的时候我个人觉得那个灰色背景框和背景很不搭。结果设置背景为无色,但是设置完了还是不行。然后我去网上看了一下,看到有两种解决的方法。
1.设置barTinColor和背景相同就看不出来那个灰色框了。因为肉眼看不出来所以这是一个障眼法。不建议大家使用哈。当然你要是觉得其他方法麻烦呢用这个还是可以的啦。
2.至于这种呢就取巧于那个subviews的方法了。这个方法跟我们的版本有关的,所以最先得判断版本了。iOS7.0以上有两层的subviews,以下有一层。刚刚好7.0呢就没有你可以直接设置背景颜色和那个barTinColor味无色就好了。接下来看代码
有两种写法。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
searchBar.tintColor: 设置输入框光标颜色
searchBar.barTintColor: 设置外层背景颜色
我通过下面的代码,试着给各种背景、边框设置不同颜色,得到的效果如图。
override func viewWillAppear(animated: Bool) {
for subView in searchBar.subviews {
for subsubView in subView.subviews {
if let bg = subsubView as? UIView {
bg.backgroundColor = UIColor.whiteColor()
bg.layer.backgroundColor = UIColor.orangeColor().CGColor
bg.layer.borderColor = UIColor.redColor().CGColor
bg.layer.borderWidth = 1
}
if let textField = subsubView as? UITextField {
textField.backgroundColor = UIColor.blueColor()
textField.layer.borderColor = UIColor.greenColor().CGColor
textField.layer.borderWidth = 1
}
}
}
searchBar.barTintColor = UIColor.yellowColor()
searchBar.tintColor = UIColor.blackColor()
}
可以看到,红色、蓝色、绿色、黑色和黄色产生了效果。
如何使 borderwidth 和 backgroundColor 生效
有一个问题是:
必须设置
borderwidth
,否则自定义的bordercolor
和backgroundColor
都不会生效。
比如注释掉borderwidth
:
override func viewWillAppear(animated: Bool) {
for subView in searchBar.subviews {
for subsubView in subView.subviews {
if let bg = subsubView as? UIView {
bg.backgroundColor = UIColor.whiteColor()
bg.layer.backgroundColor = UIColor.orangeColor().CGColor
bg.layer.borderColor = UIColor.redColor().CGColor
//bg.layer.borderWidth = 1
}
if let textField = subsubView as? UITextField {
textField.backgroundColor = UIColor.blueColor()
textField.layer.borderColor = UIColor.greenColor().CGColor
//textField.layer.borderWidth = 1
}
}
}
searchBar.barTintColor = UIColor.yellowColor()
searchBar.tintColor = UIColor.blackColor()
}
会发现红色和绿色不见了。
背后的 view
仔细看
这个时候在文本输入框的角落出现了橙色。
这说明,searchBar 的子视图中,除了一个作为整体背景的UIView
和文本输入框之外,还有一个和文本输入框相同大小的UIView
。
所以我把文本输入框的背景色设置为clearColor
,然后得到下图:
这个 view 具体作用是什么,之后再来和另外几个一起一个一个弄清楚。
另一个问题:当 searchBarStyle 为 UISearchBarStyle.Minimal 时
searchBarStyle
默认为Prominent
,你可以自己修改为Minimal
,它的样式更简洁。
但是,当我切换成Minimal
之后,结果变成这样了:
可以看到,searchBar.barTintColor = UIColor.yellowColor()
没有生效。
暂时不知道为什么,我猜是Apple
故意设置的不允许修改。
默认的黑色边框
当我把 search bar 背景颜色设置为我想要的浅灰色之后,又一个问题出现了。
居然有默认的边框。而且我取色看了一下,在背景颜色不同时,边框颜色值还不一样……
我尝试设置borderwidth
为0
,可是边框依旧显示。(不信你试试:D )
所以我猜这可能是,当 search bar 没有边框的时候自动添加的强制性边框,用于和其他元素区别吧。
不过刚才已经找到修改边框颜色的方法了。所以用上面的方法修改即可,记得设置borderWidth
。
这里也有一点要注意:
因为上面我们并没有把每个UIView
单独拿出来进行操作,而是遍历子视图里的所有UIView
修改边框,这就导致包含的textField
边框也被修改为背景层的边框颜色。假如我们自定义的textField
边框颜色和背景层边框颜色不一样,那么这就不是我们想要的。记得在if let bg = subsubView as? UIView
后面再执行if let textField = subsubView as? UITextField
。
override func viewWillAppear(animated: Bool) {
for subView in searchBar.subviews {
print(subView)
for subsubView in subView.subviews {
print(subsubView)
if let bg = subsubView as? UIView {
bg.layer.borderColor = UIColor(red: 242/250, green: 242/250, blue: 242/250, alpha: 1).CGColor
bg.layer.borderWidth = 1
}
if let textField = subsubView as? UITextField {
textField.layer.borderWidth = 0
}
}
}
searchBar.barTintColor = UIColor(red: 242/250, green: 242/250, blue: 242/250, alpha: 1)
}
最后附一张我的截图 : D
我们要自定义原生控件的外观, 比如说要更改输入框背景颜色, 需要找到UITextField 并更改属性即可,
方法一:
遍历查找,
UIColor *innerColor = [UIColor redColor];
for (UIView* subview in [[self.searchBar.subviews lastObject] subviews]) {
if ([subview isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField*)subview;
textField.backgroundColor = innerColor;
}
}
方法二:
使用 UIAppearance 协议更改
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setBackgroundColor:innerColor];
更改的时候遇到一个问题: 更改不生效, 遍历时, 发现只有当 UISearchBar所在的Viewcontroller的View 加载完后才能找到, 这时候需要在初始化UISearchBar后,
调用 layoutSubviews ,让UISeachBar 加载所有子VIew, 即: [searchBar layoutSubviews] .
个人理解为 UISearchBar 为懒加载.
// 去掉边框 改内部背景 添加右边清除btn
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(40, self.selectTimeLa.bottom+30, SceneWidth-80, 40)];
_searchBar.placeholder = @"输入关键字";
_searchBar.delegate = self;
// _searchBar.barTintColor = [UIColor colorWithWhite:0.95 alpha:1.0];
// _searchBar.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1.0];
[_searchBar setBackgroundImage:[[UIImage alloc] init]];
[self.headView addSubview:_searchBar];
_searchBar.searchBarStyle = UISearchBarStyleMinimal;
_searchBar.enablesReturnKeyAutomatically = YES ;
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version == 7.0) {
_searchBar.backgroundColor = [UIColor clearColor];
_searchBar.barTintColor = [UIColor clearColor];
}else{
for(int i = 0 ;i < _searchBar.subviews.count;i++){
UIView * backView = _searchBar.subviews[i];
if ([backView isKindOfClass:NSClassFromString(@"UISearchBarBackground")] == YES) {
[backView removeFromSuperview];
[_searchBar setBackgroundColor:[UIColor clearColor]];
break;
}else{
NSArray * arr = _searchBar.subviews[i].subviews;
for(int j = 0;j<arr.count;j++ ){
UIView * barView = arr[i];
if ([barView isKindOfClass:NSClassFromString(@"UISearchBarBackground")] == YES) {
[barView removeFromSuperview];
[_searchBar setBackgroundColor:[UIColor clearColor]];
break;
}
}
}
}
}
UIButton* btnClear = [[UIButton alloc] initWithFrame:CGRectMake(self.searchBar.width-40, 0, 40, 40)];
[btnClear addTarget:self action:@selector(clearTextAcion:) forControlEvents:UIControlEventTouchUpInside];
[self.searchBar addSubview:btnClear];
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
NSString *aa=@"11";
if ([_searchBar.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].length == 0 || [aa isEqualToString:[_searchBar.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]]) {
return;
}
[searchBar resignFirstResponder];
}