UISearchBar的使用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UISearchBar的使用相关的知识,希望对你有一定的参考价值。
self.searchBar = [[UISearchBar alloc] init];
self.searchBar.barTintColor = BLUE_COLOR;
1.0 更改背景色 / 添加边框 /
UIView *searView = [[self.searchBar.subviews firstObject].subviews lastObject];
searView.backgroundColor = BLUE_COLOR;
self.searchBar.layer.cornerRadius = 10;
self.searchBar.layer.masksToBounds = YES;
CGColorSpaceRef colorSpace2 = CGColorSpaceCreateDeviceRGB();
CGColorRef colorref2 = CGColorCreate(colorSpace2,(CGFloat[]){ 255.0/255.0, 255.0/255.0, 255.0/255.0, 1 });
CGColorSpaceRelease(colorSpace2);
[self.searchBar.layer setBorderColor:colorref2];//边框颜色
[self.searchBar.layer setBorderWidth:0.5];//边框宽度
CGColorRelease(colorref2);
searView.backgroundColor = BLUE_COLOR;
self.searchBar.layer.cornerRadius = 10;
self.searchBar.layer.masksToBounds = YES;
CGColorSpaceRef colorSpace2 = CGColorSpaceCreateDeviceRGB();
CGColorRef colorref2 = CGColorCreate(colorSpace2,(CGFloat[]){ 255.0/255.0, 255.0/255.0, 255.0/255.0, 1 });
CGColorSpaceRelease(colorSpace2);
[self.searchBar.layer setBorderColor:colorref2];//边框颜色
[self.searchBar.layer setBorderWidth:0.5];//边框宽度
CGColorRelease(colorref2);
self.searchBar.placeholder = @"查找公司";
2.0 遍历更换 放大镜 和 占位文字颜色
for (UIView* subview in [[self.searchBar.subviews lastObject] subviews]) {
if ([subview isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField*)subview;
// textField.textColor = [UIColor redColor]; //修改输入字体的颜色
// [textField setBackgroundColor:[UIColor grayColor]]; //修改输入框的颜色
[textField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"]; //修改placeholder的颜色
UIImage *image = [UIImage imageNamed:@"minge_edit"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[textField setLeftView:imageView];
} else if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
{
[subview removeFromSuperview];
}
if ([subview isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField*)subview;
// textField.textColor = [UIColor redColor]; //修改输入字体的颜色
// [textField setBackgroundColor:[UIColor grayColor]]; //修改输入框的颜色
[textField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"]; //修改placeholder的颜色
UIImage *image = [UIImage imageNamed:@"minge_edit"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[textField setLeftView:imageView];
} else if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
{
[subview removeFromSuperview];
}
}
[self addSubview:self.searchBar];
以上是关于UISearchBar的使用的主要内容,如果未能解决你的问题,请参考以下文章