iOS中的UISearchBar搜索框组件基础使用指南
uisearchbar也是ios开发常用控件之一,点进去看看里面的属性barstyle、text、placeholder等等。但是这些属性显然不足矣满足我们的开发需求。比如:修改placeholder的颜色、修改uisearchbar上面的uitextfield的背景颜色、修改uitextfield上面的照片等等。
为了实现上述的需求,最好写一个uisearchbar的子类就叫lssearchbar吧
lssearchbar.h如下:
#import <uikit/uikit.h>
@interface lssearchbar : uisearchbar
@end
lssearchbar.m如下:
#import "lssearchbar.h"
@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移去
seachbar=[[uisearchbar alloc] init];
seachbar.backgroundcolor=[uicolor clearcolor];
for (uiview *subview in seachbar.subviews){
if ([subview iskindofclass:nsclassfromstring(@"uisearchbarbackground")]) {
[subview removefromsuperview];
break;
}
}
uisearchbar文字颜色改变
1. 在ios的7访问文本字段,你必须在水平重申更多。更改您的代码像这样
for (uiview *subview in self.searchbar.subviews)
{
for (uiview *secondlevelsubview in subview.subviews){
if ([secondlevelsubview iskindofclass:[uitextfield class]])
{
uitextfield *searchbartextfield = (uitextfield *)secondlevelsubview;
//set font color here
searchbartextfield.textcolor = [uicolor blackcolor];
break;
}
}
}
或可以设置的tintcolor适用于关键在search bar。 使用tintcolor至着色前景 使用bartintcolor要着色的栏背景。 在ios系统v7.0,的uiview的子类派生的基类行为tintcolor。见tintcolor在为uiview的水平 苹果文件
2. 可以通过设置文字的颜色
[[uitextfield appearancewhencontainedin:[uisearchbar class], nil] settextcolor:[uicolor bluecolor]];
3. 虽然这是真的,uiappearance协议是一个“公开的api,”这不是真的,uitextfield的支持这一点。 如果你看一看uitextfield.h并查找字符串“ui_appearance_selector”,你会看到它有这个字符串的任何实例。如果你看的uibutton codego.net,你会发现不少-这些都是由该uiappearance api正式支持的属性。这是众所周知的,uitextfield的是不支持的uiappearance api,所以在桑迪普的答案代码并不总是可行的,它实际上不是最好的方法。 这是帖子的链接: 正确的做法是-遍历子视图(或子视图主要用于ios7的子视图)和手动设置。否则,您将有不可靠的结果。但你可以创建一个类别的uisearchbar并添加settextcolor:(*的uicolor)示例:
- (void)settextcolor:(uicolor*)color
{
for (uiview *v in self.subviews)
{
if([environment isversion7orhigher]) //checks uidevice#systemversion
{
for(id subview in v.subviews)
{
if ([subview iskindofclass:[uitextfield class]])
{
((uitextfield *)subview).textcolor = color;
}
}
}
else
{
if ([v iskindofclass:[uitextfield class]])
{
((uitextfield *)v).textcolor = color;
}
}
}
}
自定义uisearchbar的背景图
- (void)layoutsubviews {
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];
}
}
if(!(searchfield == nil)) {
searchfield.textcolor = [uicolor whitecolor];
[searchfield.leftview sethidden:yes];
[searchfield setbackground: [uiimage imagenamed:@"searchbarbackground.png"] ];
[searchfield setborderstyle:uitextborderstylenone];
}
[super layoutsubviews];
}