文章转自: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:[UIColorwhiteColor] forKeyPath:@"_placeholderLabel.textColor"];
5、//字体颜色(textField对应下文)
[textFieldsetTextColor:[UIColorwhiteColor]];
6、// 设置文本框背景
[textFieldsetBackground:searchBarBgImage];
[textField setBackgroundColor:[UIColorcolorWithPatternImage:searchBarBgImage]];
MySearchBar.h
- #import <UIKit/UIKit.h>
- #define kBgTextFieldImageName @"search_bar_bg.png"
- @interface MySearchBar : UISearchBar <UISearchBarDelegate>
- - (void)changeBarTextfieldWithColor:(UIColor *)color bgImageName:(NSString *)bgImageName;
- - (void)changeBarCancelButtonWithColor:(UIColor *)textColor bgImageName:(NSString *)bgImageName;
- @end
MySearchBar.m
- #import "MySearchBar.h"
- #import "CommonMethods.h"
- @implementation MySearchBar
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- UIColor *color = [UIColor colorWithRed:255/255.0 green:128/255.0 blue:0.0 alpha:1];
- [self changeBarTextfieldWithColor: color bgImageName: kBgTextFieldImageName];
- [self changeBarCancelButtonWithColor:[UIColor whiteColor] bgImageName: nil nil];
- }
- return self;
- }
- - (void)changeBarTextfieldWithColor:(UIColor *)color bgImageName:(NSString *)bgImageName
- {
- self.tintColor=color;
- UITextField *textField;
- if ([[[UIDevice currentDevice] systemVersion] floatValue] > 6.1f) {
- for (UIView *subv in self.subviews) {
- for (UIView* view in subv.subviews) {
- if ([view isKindOfClass:NSClassFromString(@"UISearchBarTextField")]) {
- textField = (UITextField*)view;
- textField.layer.borderWidth=1;
- textField.layer.cornerRadius=6;
- textField.layer.borderColor=color.CGColor;
- break;
- }
- }
- }
- }else{
- for (UITextField *subv in self.subviews) {
- if ([subv isKindOfClass:[UITextField class]]) {
- textField = (UITextField*)subv;
- break;
- }
- }
- }
- // 设置文本框背景
- NSArray *subs = self.subviews;
- if ([[[UIDevice currentDevice] systemVersion] floatValue] > 6.1f) { // ios 7
- for (int i = 0; i < [subs count]; i++) {
- UIView* subv = (UIView*)[self.subviews objectAtIndex:i];
- for (UIView* subview in subv.subviews) {
- if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
- {
- [subview setHidden:YES];
- [subview removeFromSuperview];
- break;
- }
- }
- }
- }else{
- for (int i = 0; i < [subs count]; i++) {
- UIView* subv = (UIView*)[self.subviews objectAtIndex:i];
- if ([subv isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
- {
- [subv removeFromSuperview];
- break;
- }
- }
- }
- UIImage *searchBarBgImage = [CommonMethods imageWithFileName: bgImageName scale:2 edgeInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
- [textField setBackground:searchBarBgImage];
- }
- - (void)changeBarCancelButtonWithColor:(UIColor *)textColor bgImageName:(NSString *)bgImageName
- {
- if ([[[UIDevice currentDevice] systemVersion] floatValue] > 6.1f) { // ios 7
-
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 grayColor] forState:UIControlStateNormal];
[cancelButton setBackgroundImage:[UIImageimageNamed:@"search_cancelbutton"] forState:UIControlStateNormal];
}
- }
-
-
- for (UIView *searchbuttons in self.subviews)
- {
- if ([searchbuttons isKindOfClass:[UIButton class]]) // ios7以下
- {
- UIButton *cancelButton = (UIButton*)searchbuttons;
- cancelButton.enabled = YES;
- [cancelButton setTitleColor:textColor forState:UIControlStateNormal];
- [cancelButton setTitleColor:textColor forState:UIControlStateSelected];
- if (bgImageName)
- {
- [cancelButton setBackgroundImage:[UIImage imageNamed:bgImageName] forState:UIControlStateNormal];
- [cancelButton setBackgroundImage:[UIImage imageNamed:bgImageName] forState:UIControlStateSelected];
- }
- break;
- }
- }
- }
- @end