//
// ZJSearchBar.h
// ZJSearchBar
//
// Created by 张建 on 2017/4/22.
// Copyright © 2017年张建. All rights reserved.
//
#import <UIKit/UIKit.h>
//自定义UISearchBar,方便项目中的使用。后续可自行进行功能添加。
@interface ZJSearchBar : UISearchBar
//搜索框
@property (nonatomic,strong)UITextField *searchBarTF;
//搜索框的背景,默认是灰色哦。系统色.是否隐藏显示
@property (nonatomic,assign)BOOL hideSearchBarBackgroundImage;
//输入框中自定义的光标颜色
@property (nonatomic,strong)UIColor *cursorColor;
//输入框中清除按钮的图片
@property (nonatomic,strong)UIImage *clearButtonImage;
//取消按钮(当showCancleButton = YES时,才可以得到)
@property (nonatomic,strong)UIButton *cancleButton;
//设置输入框中和提示文字 是否居中。(NO是不居中)
@property (nonatomic, assign, setter = setHasCentredPlaceholder:) BOOL hasCentredPlaceholder;
#import "ZJSearchBar.h"
@implementation ZJSearchBar
//设置输入框
- (UITextField *)searchBarTF{
//获取输入框
_searchBarTF = [self valueForKey:@"searchField"];
return _searchBarTF;
}
//设置输入框中的光标的颜色,可以自定义的哦
- (void)setCursorColor:(UIColor *)cursorColor{
if (cursorColor) {
_cursorColor = cursorColor;
//1.获取输入框
UITextField *searchField = self.searchBarTF;
if (searchField) {
//2.光标颜色
[searchField setTintColor:cursorColor];
}
}
}
//设置清除按钮的图标
- (void)setClearButtonImage:(UIImage *)clearButtonImage{
if (clearButtonImage) {
_clearButtonImage = clearButtonImage;
//1.获取输入框
UITextField *searchField = self.searchBarTF;
if (searchField) {
//清除按钮的图片
UIButton *button = [searchField valueForKey:@"_clearButton"];
[button setImage:clearButtonImage forState:UIControlStateNormal];
searchField.clearButtonMode = UITextFieldViewModeWhileEditing;
}
}
}
//隐藏背景图
- (void)setHideSearchBarBackgroundImage:(BOOL)hideSearchBarBackgroundImage{
if (hideSearchBarBackgroundImage) {
_hideSearchBarBackgroundImage = hideSearchBarBackgroundImage;
self.backgroundImage = [[UIImage alloc]init];
}
}
//获取取消的按钮
- (UIButton *)cancleButton{
self.showsCancelButton = YES;
for (UIView *view in [[self.subviews lastObject]subviews]) {
if ([view isKindOfClass:[UIButton class]]) {
_cancleButton = (UIButton *)view;
}
}
return _cancleButton;
}
- (instancetype)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame]))
{
self.hasCentredPlaceholder = YES;
}
return self;
}
//设置搜索框中搜索