自定义搜索框

自定义搜索框

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“`

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值