自定义UISearchBar,方便项目中的使…

//

//  ZJSearchBar.h

//  ZJSearchBar

//

//  Created by 张建 on 2017/4/22.

//  Copyright © 2017 张建. All rights reserved.

//


#import


//自定义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;


@end


//

//  ZJSearchBar.m

//  ZJSearchBar

//

//  Created by 张建 on 2017/4/22.

//  Copyright © 2017 张建. All rights reserved.

//


#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;

}


//设置搜索框中搜索🔍和提示文字的位置  (居左)

- (void)setHasCentredPlaceholder:(BOOL)hasCentredPlaceholder

{

    _hasCentredPlaceholder = hasCentredPlaceholder;

    

    SEL centerSelector = NSSelectorFromString([NSString stringWithFormat:@"%@%@", @"setCenter", @"Placeholder:"]);

    if ([self respondsToSelector:centerSelector])

    {

        NSMethodSignature *signature = [[UISearchBar class] instanceMethodSignatureForSelector:centerSelector];

        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];

        [invocation setTarget:self];

        [invocation setSelector:centerSelector];

        [invocation setArgument:&_hasCentredPlaceholder atIndex:2];

        [invocation invoke];

    }

}


@end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值