UISearchBar也是iOS开发常用控件之一,点进去看看里面的属性barStyle、text、placeholder等等。但是这些属性显然不足矣满足我们的开发需求。比如:修改placeholder的颜色、修改UISearchBar上面的UITextfield的背景颜色、修改UITextfield上面的照片等等。
为了实现上述的需求,最好写一个UISearchBar的子类就叫LSSearchBar吧
LSSearchBar.h如下:
@interface LSSearchBar : UISearchBar
@end
LSSearchBar.m如下:
@implementation LSSearchBar
- (void)layoutSubviews {
[super layoutSubviews];
//通过遍历self.subviews找到searchField
UITextField *searchField;
NSUInteger numViews = [self.subviews count];
for(int i = 0; i < numViews; i++) {
if([[self.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) {
searchField = [self.subviews objectAtIndex:i];
}
}
//如果上述方法找不到searchField,那就试试下面的方法吧
if (searchField == nil) {
NSArray *arraySub = [self subviews];
UIView *viewSelf = [arraySub objectAtIndex:0];
NSArray *arrayView = [viewSelf subviews];
for(int i = 0; i < arrayView.count; i++) {
if([[arrayView objectAtIndex:i] isKindOfClass:[UITextField class]]) {
searchField = [arrayView objectAtIndex:i];
}
}
}
if(!(searchField == nil)) {
//设置颜色
searchField.textColor = [UIColor whiteColor];
//设置背景颜色
[searchField setBackground: [UIImage imageNamed:@"searchbar"] ];
[searchField setBorderStyle:UITextBorderStyleNone];
//设置placeholder的颜色
[searchField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
//设置searchField上的照片
UIImage *image = [UIImage imageNamed:@"search"];
UIImageView *iView = [[UIImageView alloc] initWithImage:image];
iView.frame = CGRectMake(0, 0, 15, 15);
searchField.leftView = iView;
}
}
@end
修改UISearchBar背景颜色
ISearchBar是由两个subView组成的,一个是UISearchBarBackGround,另一个是UITextField. 要IB中没有直接操作背景的属性。方法是直接将 UISearchBarBackGround移去
UISearchBar文字颜色改变
1. 在iOS的7访问文本字段,你必须在水平重申更多。更改您的代码像这样
自定义UISearchBar的背景图
新闻热点
疑难解答