美文网首页IOS专辑iOS Developer
iOS开发之-自定义搜索框-项目必备

iOS开发之-自定义搜索框-项目必备

作者: A迷城 | 来源:发表于2017-05-24 20:56 被阅读307次

在实际开发中,搜索框的样式多种多样,iOS 中原生的搜索框有许多UI 会进行修改。所以在项目中,自己进行自定义,你会用到的额。直接用吧。更多文章,也可以关注新浪博客http://blog.sina.com.cn/resoftios

//

// ZJSearchBar.h

// ZJSearchBar

//

// Created by张建on2017/4/22.

// Copyright © 2017年张建. All rightsreserved.

//

#import

//自定义UISearchBar,方便项目中的使用。后续可自行进行功能添加。

@interfaceZJSearchBar :UISearchBar

//搜索框

@property(nonatomic,strong)UITextField*searchBarTF;

//搜索框的背景,默认是灰色哦。系统色.是否隐藏显示

@property(nonatomic,assign)BOOLhideSearchBarBackgroundImage;

//输入框中自定义的光标颜色

@property(nonatomic,strong)UIColor*cursorColor;

//输入框中清除按钮的图片

@property(nonatomic,strong)UIImage*clearButtonImage;

//取消按钮(当showCancleButton= YES时,才可以得到)

@property(nonatomic,strong)UIButton*cancleButton;

//设置输入框中🔍和提示文字是否居中。(NO是不居中)

@property(nonatomic,assign,setter=setHasCentredPlaceholder:)BOOLhasCentredPlaceholder;

@end

//

// ZJSearchBar.m

// ZJSearchBar

//

// Created by张建on2017/4/22.

// Copyright © 2017年张建. All rightsreserved.

//

#import"ZJSearchBar.h"

@implementationZJSearchBar

//设置输入框

-(UITextField*)searchBarTF{

//获取输入框

_searchBarTF=[selfvalueForKey:@"searchField"];

return_searchBarTF;

}

//设置输入框中的光标的颜色,可以自定义的哦

-(void)setCursorColor:(UIColor*)cursorColor{

if(cursorColor){

_cursorColor=cursorColor;

//1.获取输入框

UITextField*searchField =self.searchBarTF;

if(searchField){

//2.光标颜色

[searchFieldsetTintColor:cursorColor];

}

}

}

//设置清除按钮的图标

-(void)setClearButtonImage:(UIImage*)clearButtonImage{

if(clearButtonImage){

_clearButtonImage=clearButtonImage;

//1.获取输入框

UITextField*searchField =self.searchBarTF;

if(searchField){

//清除按钮的图片

UIButton*button =[searchFieldvalueForKey:@"_clearButton"];

[buttonsetImage:clearButtonImageforState:UIControlStateNormal];

searchField.clearButtonMode=UITextFieldViewModeWhileEditing;

}

}

}

//隐藏背景图

-(void)setHideSearchBarBackgroundImage:(BOOL)hideSearchBarBackgroundImage{

if(hideSearchBarBackgroundImage){

_hideSearchBarBackgroundImage=hideSearchBarBackgroundImage;

self.backgroundImage=[[UIImagealloc]init];

}

}

//获取取消的按钮

-(UIButton*)cancleButton{

self.showsCancelButton=YES;

for(UIView*viewin[[self.subviewslastObject]subviews]){

if([viewisKindOfClass:[UIButtonclass]]){

_cancleButton=(UIButton*)view;

}

}

return_cancleButton;

}

-(instancetype)initWithFrame:(CGRect)frame

{

if((self=[superinitWithFrame:frame]))

{

self.hasCentredPlaceholder=YES;

}

returnself;

}

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

-(void)setHasCentredPlaceholder:(BOOL)hasCentredPlaceholder

{

_hasCentredPlaceholder=hasCentredPlaceholder;

SELcenterSelector=NSSelectorFromString([NSStringstringWithFormat:@"%@%@",@"setCenter",@"Placeholder:"]);

if([selfrespondsToSelector:centerSelector])

{

NSMethodSignature*signature =[[UISearchBarclass]instanceMethodSignatureForSelector:centerSelector];

NSInvocation*invocation =[NSInvocationinvocationWithMethodSignature:signature];

[invocationsetTarget:self];

[invocationsetSelector:centerSelector];

[invocationsetArgument:&_hasCentredPlaceholderatIndex:2];

[invocationinvoke];

}

}

@end

相关文章

网友评论

    本文标题:iOS开发之-自定义搜索框-项目必备

    本文链接:https://www.haomeiwen.com/subject/wggoxxtx.html