您的位置:首页 > 其它

自定义搜索框

2015-10-13 11:40 344 查看
自定义搜索框

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“`
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: