iOS自定义searchBar的两种方法

iOS自定义searchBar的两种方法
  • 首先可以用textfield的leftview做文章
    UITextField *textfield = [[UITextField alloc] initWithFrame:CGRectMake(20, 80, self.view.frame.size.width - 40, 30)];
    textfield.alpha = 0.8;
    textfield.layer.cornerRadius = 5;
    textfield.layer.masksToBounds = YES;
    textfield.placeholder = @"搜索";
    textfield.textColor = [UIColor darkGrayColor];
    textfield.backgroundColor = [UIColor colorWithRed:235 / 255.0 green:235 / 255.0 blue:235 / 255.0 alpha:1];
//    一定要这样写,否则出来的图标会贴近左侧哦
//    先不用给定frame
    UIImageView *searchIcon = [[UIImageView alloc] init];
    searchIcon.image = [UIImage imageNamed:@"home-search"];
//    设置 图片填充为中心填充,从中心点拉伸填充 contents remain same size. positioned adjusted.
    searchIcon.contentMode = UIViewContentModeCenter;
//    设置图片的frame大小
    searchIcon.size = CGSizeMake(30, 30);
    textfield.leftView = searchIcon;
//    设置左侧view 总是在 UITextFieldViewModeAlways
    textfield.leftViewMode = UITextFieldViewModeAlways;
    textfield.font = Font(13);
  • 其次可以自定义view的方式实现
  1. .h文件
#import 

@class CustomSeachBar;

@protocol CustomSearchBarDelegate 
@optional

- (BOOL)searchBarShouldBeginEditing:(CustomSeachBar *)searchBar;

- (void)searchBarTextDidBeginEditing:(CustomSeachBar *)searchBar;

- (BOOL)searchBarShouldEndEditing:(CustomSeachBar *)searchBar;

- (void)searchBarTextDidEndEditing:(CustomSeachBar *)searchBar;

- (void)searchBar:(CustomSeachBar *)searchBar textDidChange:(NSString *)searchText;

- (void)searchBarSearchButtonClicked:(CustomSeachBar *)searchBar;//确定按钮

- (void)searchBarCancelButtonClicked:(CustomSeachBar *)searchBar;
@optional


@end
@interface CustomSeachBar : UIView

//文本的颜色
@property (nonatomic,strong)UIColor * textColor;
//字体
@property (nonatomic,strong)UIFont * searBarFont;
//内容
@property (nonatomic,strong)NSString * text;
//背景色
@property (nonatomic,strong)UIColor * searBarColor;
//默认文本
@property (nonatomic,copy)NSString * placeholder;
//默认文本的颜色
@property (nonatomic,strong)UIColor * placeholdesColor;
//默认文本字体大小
@property (nonatomic,strong)UIFont * placeholdesFont;
//是否弹出键盘
@property (nonatomic,assign)BOOL isbecomeFirstResponder;
//设置右边按钮的样式
@property (nonatomic,strong)UIImage * deleteImage;
//设置代理
@property (nonatomic,weak)iddelegate;
@end
  1. .m文件
#import "CustomSeachBar.h"

@interface CustomSeachBar()

@property (nonatomic, strong)UITextField * textField;
@property (nonatomic, strong)UIImageView * leftView;
@property (nonatomic, strong)UIButton * deleteBtn;

@end


@implementation CustomSeachBar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    
        [self initAllViews];

    }
    return self;
}
- (void)initAllViews
{
    self.layer.cornerRadius = 5;
    self.layer.masksToBounds = YES;
    
    if (_leftView == nil) {
        _leftView = [[UIImageView alloc] initWithFrame:CGRectMake(10, (self.frame.size.height - 17) / 2.0, 17, 17)];
        _leftView.image = [UIImage imageNamed:@"home-search"];
        [self addSubview:_leftView];
    }
    
    if (_textField == nil) {
        _textField = [[UITextField alloc] initWithFrame:CGRectMake(_leftView.frame.size.width + _leftView.frame.origin.x + 10, 2, self.frame.size.width - (_leftView.frame.size.width + _leftView.frame.origin.x + 10) - (self.frame.size.height - 4) , self.frame.size.height - 4)];
        _textField.backgroundColor = [UIColor yellowColor];
        _textField.returnKeyType = UIReturnKeyDone;
        _textField.delegate = self;
        [_textField addTarget:self action:@selector(textFieldValueChange:) forControlEvents:UIControlEventEditingChanged];
        [self addSubview:_textField];
    }
    
    if (_deleteBtn == nil) {
        _deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _deleteBtn.frame = CGRectMake(self.frame.size.width - self.frame.size.height - 4, 2, self.frame.size.height - 4, self.frame.size.height - 4);
        [_deleteBtn setImage:[UIImage imageNamed:@"home-close"] forState:UIControlStateNormal];
        [_deleteBtn addTarget:self action:@selector(deleteClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_deleteBtn];
    }
}
- (void)setIsbecomeFirstResponder:(BOOL)isbecomeFirstResponder
{
    _isbecomeFirstResponder = isbecomeFirstResponder;
    if (_isbecomeFirstResponder) {
        [_textField becomeFirstResponder];
    }
}

- (void)setSearBarColor:(UIColor *)searBarColor
{
    _searBarColor = searBarColor;
    self.backgroundColor = _searBarColor;
    _textField.backgroundColor = _searBarColor;
    _leftView.backgroundColor = _searBarColor;
    _deleteBtn.backgroundColor = _searBarColor;
}

- (void)setSearBarFont:(UIFont *)searBarFont
{
    _searBarFont = searBarFont;
    _textField.font = _searBarFont;
}

- (void)setTextColor:(UIColor *)textColor
{
    _textColor = textColor;
    _textField.textColor = _textColor;
}

- (void)setPlaceholder:(NSString *)placeholder
{
    _placeholder = placeholder;
    _textField.placeholder = _placeholder;
}

- (void)setPlaceholdesColor:(UIColor *)placeholdesColor
{
    
    _placeholdesColor = placeholdesColor;
    [_textField setValue:_placeholdesColor forKeyPath:@"_placeholderLabel.textColor"];
    
}

- (void)setPlaceholdesFont:(UIFont *)placeholdesFont
{
    _placeholdesFont = placeholdesFont;
    [_textField setValue:_placeholdesFont forKeyPath:@"_placeholderLabel.font"];
    
}

- (void)setText:(NSString *)text
{
    _text = text;
    _textField.text = _text;
}

- (void)setDeleteImage:(UIImage *)deleteImage
{
    _deleteImage = deleteImage;
    [_deleteBtn setImage:_deleteImage forState:UIControlStateNormal];
}

#pragma mark - deleteClick
- (void)deleteClick:(UIButton *)delete
{
    NSLog(@"删除");
    if ([_delegate respondsToSelector:@selector(searchBarCancelButtonClicked:)]) {
        [self.delegate searchBarCancelButtonClicked:self];
    }
}

#pragma mark - textFieldValueChange
- (void)textFieldValueChange:(UITextField *)textField
{
    _text = textField.text;
    if ([_delegate respondsToSelector:@selector(searchBar:textDidChange:)]) {
        [self.delegate searchBar:self textDidChange:textField.text];
    }
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if ([_delegate respondsToSelector:@selector(searchBarShouldBeginEditing:)]) {
        [self.delegate searchBarShouldBeginEditing:self];
    }
    
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if ([_delegate respondsToSelector:@selector(searchBarTextDidBeginEditing:)]) {
        [self.delegate searchBarTextDidBeginEditing:self];
    }
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    if ([_delegate respondsToSelector:@selector(searchBarShouldEndEditing:)]) {
        [self.delegate searchBarShouldEndEditing:self];
    }
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if ([_delegate respondsToSelector:@selector(searchBarTextDidEndEditing:)]) {
        [self.delegate searchBarTextDidEndEditing:self];
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if ([_delegate respondsToSelector:@selector(searchBarSearchButtonClicked:)]) {
        [self.delegate searchBarSearchButtonClicked:self];
    }
    return YES;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

总结,方法一简单好用快速,方法二可扩展性极高。根据需求选择吧。

你可能感兴趣的:(iOS自定义searchBar的两种方法)