自定义搜索框
2015-10-13 11:40
344 查看
自定义搜索框
自定义的文本框必须在-(void)layoutSubviews中才能自定义 (即子控件必须在layourSubviews中设置,而SearchBar可以直接设置:placeholder,外背景)
随意设置即可
字体颜色和大小无法设置,但是外边框可以设置
UISearchBar *searchBar;
//**取出文本框
UITextField *searchField = [_searchBar valueForKey:@”_searchField”];
//**输入字体颜色
[searchField setTextColor:[UIColor redColor]];
//**字体大小
[searchField setFont:[UIFont systemFontOfSize:20]];
//**搜索图标
UIImageView *iView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@”“]];
[iView setFrame:CGRectMake(0.0, 0.0, 36.0, 16.0)];
searchField.leftView = iView;
//**放右边的话 必须写这一句,左边不需要
// searchField.rightViewMode = UITextFieldViewModeAlways;
外边框颜色:
UIView *outView = [[UIView alloc] initWithFrame:self.bounds];
[outView setBackgroundColor:[UIColor orangeColor]];
[self insertSubview:outView atIndex:1];
[_searchBar setPlaceholder:@”会输入吗”];
“`
-(void)layoutSubviews
{
[super layoutSubviews];
//**放右边的话写这一句,左边不需要
searchField.rightViewMode = UITextFieldViewModeAlways;
}
@end“`
1、自定义
自定义的文本框必须在-(void)layoutSubviews中才能自定义 (即子控件必须在layourSubviews中设置,而SearchBar可以直接设置:placeholder,外背景)
2、storyboard
随意设置即可
3、纯代码
字体颜色和大小无法设置,但是外边框可以设置
总结: 通过对比三种searchBar的设置,如果不使用storyboard,发现自定义相对于来说比较好用,严重不推荐纯代码的设置。
UISearchBar *searchBar;
搜索框的几个属性: 1、文本UITextField(包括:文本输入颜色,输入字体大小,搜索图标) 注意:禁止两个searchBar使用同一个view,会不显示。 注意:::自定义的文本框必须在-(void)layoutSubviews中才能生效
//**取出文本框
UITextField *searchField = [_searchBar valueForKey:@”_searchField”];
//**输入字体颜色
[searchField setTextColor:[UIColor redColor]];
//**字体大小
[searchField setFont:[UIFont systemFontOfSize:20]];
//**搜索图标
UIImageView *iView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@”“]];
[iView setFrame:CGRectMake(0.0, 0.0, 36.0, 16.0)];
searchField.leftView = iView;
//**放右边的话 必须写这一句,左边不需要
// searchField.rightViewMode = UITextFieldViewModeAlways;
2、外边框 内边框 注意:::禁止两个searchBar使用同一个view,会不显示
外边框颜色:
UIView *outView = [[UIView alloc] initWithFrame:self.bounds];
[outView setBackgroundColor:[UIColor orangeColor]];
[self insertSubview:outView atIndex:1];
3、placeholder
[_searchBar setPlaceholder:@”会输入吗”];
#import "ViewController.h" #import "SearchBar.h" @interface ViewController ()<UISearchBarDelegate> //**自定义searchBar @property (nonatomic,strong)SearchBar *customBar; //**系统自带 @property (strong, nonatomic) IBOutlet UISearchBar *xibBar; //**纯代码 @property (nonatomic,strong)UISearchBar *codeBar; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //**自定义 self.customBar = [[SearchBar alloc] initWithFrame:CGRectMake(5, 130, self.view.frame.size.width-10, 50)]; self.customBar.layer.cornerRadius = 20; [self.view addSubview:self.customBar]; //**纯代码 self.codeBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 250, self.view.frame.size.width, 50)]; [self.view addSubview:self.codeBar]; [self Cbar]; } -(void)Cbar { //**xib UITextField *searchField = [self.xibBar valueForKey:@"_searchField"]; [searchField setTextColor:[UIColor blueColor]]; [searchField setFont:[UIFont systemFontOfSize:18]]; [self.xibBar setPlaceholder:@"xib请输入"]; #warning //**自定义在此不会生效 只能在layoutSubviews中设置字体颜色 UITextField *searchField2 = [self.customBar valueForKey:@"_searchField"]; // [searchField2 setTextColor:[UIColor greenColor]]; // [searchField2 setFont:[UIFont systemFontOfSize:20]]; [self.customBar setPlaceholder:@"自定义===="]; #warning //**纯代码 字体颜色和字体大小无法设置 但是外边框可以设置 UITextField *searchField3 = [self.codeBar valueForKey:@"_searchField"]; [searchField3 setTextColor:[UIColor orangeColor]]; [searchField3 setFont:[UIFont systemFontOfSize:20]]; [self.codeBar setPlaceholder:@"纯代码请输入"]; #warning //自定义外背景 禁止两个searchBar使用同一个外背景,会不显示 UIView *outV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)]; [outV setBackgroundColor:[UIColor blueColor]]; [self.xibBar insertSubview:outV atIndex:1]; UIView *outV1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)]; [outV1 setBackgroundColor:[UIColor redColor]]; [self.customBar insertSubview:outV1 atIndex:1]; UIView *outV2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)]; [outV2 setBackgroundColor:[UIColor orangeColor]]; [self.codeBar insertSubview:outV2 atIndex:1]; } @end
自定义SearchBar
“`
import “SearchBar.h”
@implementation SearchBar-(void)layoutSubviews
{
[super layoutSubviews];
UITextField *searchField; searchField = [self valueForKey:@"_searchField"]; [searchField setTextColor:[UIColor redColor]]; searchField.clearButtonMode = UITextFieldViewModeWhileEditing; UIImageView *iView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"2"]]; [iView setFrame:CGRectMake(0.0, 0.0, 20.0, 20.0)]; searchField.leftView = iView; [self setPlaceholder:@"自定义"];
//**放右边的话写这一句,左边不需要
searchField.rightViewMode = UITextFieldViewModeAlways;
}
@end“`
相关文章推荐
- 安装SASS
- 项目没有错但是有红叉号
- C#导出网站功能实例代码讲解
- hbase常用shell命令
- js call与apply方法
- 一共81个,开源大数据处理工具汇总(下),包括日志收集系统/集群管理/RPC等
- windows ipython 出现ImportError: No module named XXXX错误
- Swift2.1 语法指南——析构过程
- Mac 将mysql路径加入环境变量
- 大数据量下高并发同步的讲解
- hicharts 时间曲线图
- 自旋锁(spin lock)与互斥量的区别
- 国家商用密码申请
- java 笔试面试易错题
- 一共81个,开源大数据处理工具汇总(上)
- 毫秒倒计时小Demo
- JSP自定义标签 - 嵌套
- PHP中empty()和isset()的区别
- oracle中的rownum、order by与分页
- Nginx报错及解决方案汇总