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

来源:互联网 发布:淘宝直播点赞花钱吗 编辑:程序博客网 时间:2024/05/16 08:51

//

// 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 =[searchField valueForKey:@"_clearButton"];

          [button setImage: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];

      [invocation setTarget:self];

      [invocation setSelector:centerSelector];

      [invocation setArgument:&_hasCentredPlaceholderatIndex:2];

      [invocation invoke];

   }

}


@end



0 0
原创粉丝点击