iOS開發(fā)之-自定義搜索框-項(xiàng)目必備

在實(shí)際開發(fā)中,搜索框的樣式多種多樣,iOS 中原生的搜索框有許多UI 會進(jìn)行修改。所以在項(xiàng)目中,自己進(jìn)行自定義,你會用到的額。直接用吧。更多文章,也可以關(guān)注新浪博客http://blog.sina.com.cn/resoftios。

//

//?ZJSearchBar.h

//?ZJSearchBar

//

//?Created by張建on2017/4/22.

//?Copyright ? 2017年張建. All rightsreserved.

//

#import

//自定義UISearchBar,方便項(xiàng)目中的使用。后續(xù)可自行進(jìn)行功能添加。

@interfaceZJSearchBar :UISearchBar

//搜索框

@property(nonatomic,strong)UITextField*searchBarTF;

//搜索框的背景,默認(rèn)是灰色哦。系統(tǒng)色.是否隱藏顯示

@property(nonatomic,assign)BOOLhideSearchBarBackgroundImage;

//輸入框中自定義的光標(biāo)顏色

@property(nonatomic,strong)UIColor*cursorColor;

//輸入框中清除按鈕的圖片

@property(nonatomic,strong)UIImage*clearButtonImage;

//取消按鈕(當(dāng)showCancleButton= YES時,才可以得到)

@property(nonatomic,strong)UIButton*cancleButton;

//設(shè)置輸入框中??和提示文字是否居中。(NO是不居中)

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

@end

//


//?ZJSearchBar.m

//?ZJSearchBar

//

//?Created by張建on2017/4/22.

//?Copyright ? 2017年張建. All rightsreserved.

//

#import"ZJSearchBar.h"

@implementationZJSearchBar

//設(shè)置輸入框

-(UITextField*)searchBarTF{

//獲取輸入框

_searchBarTF=[selfvalueForKey:@"searchField"];

return_searchBarTF;

}

//設(shè)置輸入框中的光標(biāo)的顏色,可以自定義的哦

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

if(cursorColor){

_cursorColor=cursorColor;

//1.獲取輸入框

UITextField*searchField =self.searchBarTF;

if(searchField){

//2.光標(biāo)顏色

[searchFieldsetTintColor:cursorColor];

}

}

}

//設(shè)置清除按鈕的圖標(biāo)

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

}

//設(shè)置搜索框中搜索??和提示文字的位置(居左)

-(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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容