自定义UISearchBar 适配IOS6和IOS7 修改放大镜图标 修改光标颜色 修改边框颜色 placeholder颜色 设置文本框背景

文章转自:http://blog.csdn.net/zhang_red/article/details/21447535


总结下常用的几点:

1、修改放大镜图标

[self setImage:[UIImage imageNamed:@"search_icon"forSearchBarIcon:UISearchBarIconSearchstate:UIControlStateNormal];  

2、修改光标颜色

self.tintColor = [UIColorwhiteColor];

3、修改边框颜色(textField对应下文)

textField.layer.borderColor = [[UIColorclearColor]CGColor];

4、//placeholder颜色(必须先设置placeholder的内容,再添此行代码)

[textFieldsetValue:[UIColorwhiteColorforKeyPath:@"_placeholderLabel.textColor"];

5、//字体颜色(textField对应下文)

[textFieldsetTextColor:[UIColorwhiteColor]];

6、// 设置文本框背景

[textFieldsetBackground:searchBarBgImage];

[textField setBackgroundColor:[UIColorcolorWithPatternImage:searchBarBgImage]];


MySearchBar.h

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #import <UIKit/UIKit.h>  
  2. #define kBgTextFieldImageName @"search_bar_bg.png"  
  3. @interface MySearchBar : UISearchBar <UISearchBarDelegate>  
  4. - (void)changeBarTextfieldWithColor:(UIColor *)color bgImageName:(NSString *)bgImageName;  
  5. - (void)changeBarCancelButtonWithColor:(UIColor *)textColor bgImageName:(NSString *)bgImageName;  
  6. @end  

MySearchBar.m

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #import "MySearchBar.h"  
  2. #import "CommonMethods.h"  
  3.   
  4. @implementation MySearchBar  
  5.   
  6. - (id)initWithFrame:(CGRect)frame  
  7. {  
  8.     self = [super initWithFrame:frame];  
  9.     if (self) {  
  10.         UIColor *color = [UIColor colorWithRed:255/255.0 green:128/255.0 blue:0.0 alpha:1];  
  11.         [self changeBarTextfieldWithColor: color bgImageName: kBgTextFieldImageName];  
  12.         [self changeBarCancelButtonWithColor:[UIColor whiteColor] bgImageName: nil nil];  
  13.     }  
  14.     return self;  
  15. }  
  16.   
  17. - (void)changeBarTextfieldWithColor:(UIColor *)color bgImageName:(NSString *)bgImageName  
  18. {  
  19.     self.tintColor=color;  
  20.       
  21.     UITextField *textField;  
  22.     if ([[[UIDevice currentDevice] systemVersion] floatValue] > 6.1f) {  
  23.         for (UIView *subv in self.subviews) {  
  24.             for (UIView* view in subv.subviews) {  
  25.                 if ([view isKindOfClass:NSClassFromString(@"UISearchBarTextField")]) {  
  26.                     textField = (UITextField*)view;  
  27.                     textField.layer.borderWidth=1;  
  28.                     textField.layer.cornerRadius=6;  
  29.                     textField.layer.borderColor=color.CGColor;  
  30.                     break;  
  31.                 }  
  32.             }  
  33.         }  
  34.     }else{  
  35.         for (UITextField *subv in self.subviews) {  
  36.             if ([subv isKindOfClass:[UITextField class]]) {  
  37.                 textField = (UITextField*)subv;  
  38.                 break;  
  39.             }  
  40.         }  
  41.     }  
  42.       
  43.     // 设置文本框背景  
  44.     NSArray *subs = self.subviews;  
  45.     if ([[[UIDevice currentDevice] systemVersion] floatValue]  > 6.1f) { // ios 7  
  46.         for (int i = 0; i < [subs count]; i++) {  
  47.             UIView* subv = (UIView*)[self.subviews objectAtIndex:i];  
  48.             for (UIView* subview in subv.subviews) {  
  49.                 if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])  
  50.                 {  
  51.                     [subview setHidden:YES];  
  52.                     [subview removeFromSuperview];  
  53.                     break;  
  54.                 }  
  55.             }  
  56.         }  
  57.     }else{  
  58.         for (int i = 0; i < [subs count]; i++) {  
  59.             UIView* subv = (UIView*)[self.subviews objectAtIndex:i];  
  60.             if ([subv isKindOfClass:NSClassFromString(@"UISearchBarBackground")])  
  61.             {  
  62.                 [subv removeFromSuperview];  
  63.                 break;  
  64.             }  
  65.         }  
  66.     }  
  67.       
  68.     UIImage *searchBarBgImage = [CommonMethods imageWithFileName: bgImageName scale:2 edgeInsets:UIEdgeInsetsMake(10101010)];  
  69.     [textField setBackground:searchBarBgImage];  
  70. }  
  71.   
  72. - (void)changeBarCancelButtonWithColor:(UIColor *)textColor bgImageName:(NSString *)bgImageName  
  73. {  
  74. if ([[[UIDevice currentDevice] systemVersion] floatValue]  > 6.1f) { // ios 7 
  75. UIButton *cancelButton;

        UIView *topView = self.subviews[0];

        for (UIView *subView in topView.subviews) {

            if ([subView isKindOfClass:NSClassFromString(@"UINavigationButton")]) {

                cancelButton = (UIButton *)subView;

            }

        }

        if (cancelButton) {

            [cancelButton setTitle:@"取消" forState:UIControlStateNormal];

            [cancelButton setTitleColor:[UIColor grayColorforState:UIControlStateNormal];

            [cancelButton setBackgroundImage:[UIImageimageNamed:@"search_cancelbutton"forState:UIControlStateNormal];

        }


  76. }



  77.     for (UIView *searchbuttons in self.subviews)  
  78.     {  
  79.           
  80.         if ([searchbuttons isKindOfClass:[UIButton class]]) // ios7以下  
  81.         {  
  82.             UIButton *cancelButton = (UIButton*)searchbuttons;  
  83.             cancelButton.enabled = YES;  
  84.             [cancelButton setTitleColor:textColor forState:UIControlStateNormal];  
  85.             [cancelButton setTitleColor:textColor forState:UIControlStateSelected];  
  86.              if (bgImageName)  
  87.              {  
  88.                  [cancelButton setBackgroundImage:[UIImage imageNamed:bgImageName] forState:UIControlStateNormal];  
  89.                  [cancelButton setBackgroundImage:[UIImage imageNamed:bgImageName] forState:UIControlStateSelected];  
  90.              }  
  91.             break;  
  92.         }  
  93.     }  
  94. }  
  95.   
  96. @end 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值