自定义UISearchBar 适配IOS6和IOS7
2014-03-18 11:31
274 查看
MySearchBar.h
MySearchBar.m
#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]; } 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 { 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
相关文章推荐
- 自定义UISearchBar 适配IOS6和IOS7
- 自定义UISearchBar 适配IOS6和IOS7 修改放大镜图标 修改光标颜色 修改边框颜色 placeholder颜色 设置文本框背景
- 自定义UISearchBar 适配IOS6和IOS7 修改放大镜图标 修改光标颜色 修改边框颜色 placeholder颜色 设置文本框背景
- ios6,ios7,ios7.1下设置UISearchbar的背景色
- ios6,ios7,ios7.1下设置UISearchbar的背景色
- uisearchbar中自定义 放大镜图标 placeolder字体颜色 文本框背景色 兼容ios7
- ios6,ios7,ios7.1下设置UISearchbar的背景色
- 关于UISearchBar在IOS6和IOS7的区别以及用法注意
- ios6,ios7,ios7.1下设置UISearchbar的背景色
- ios6,ios7,ios7.1下设置UISearchbar的背景色
- UISearchBar 完美自定义,适配6.0、7.0、7.1
- ios6,ios7,ios7.1下设置UISearchbar的背景色
- ios6,ios7,ios7.1下设置UISearchbar的背景色
- iOS6和iOS7代码的适配(4)——tableView
- iOS7 界面适配-NavigationBar StateBar
- iOS6和iOS7代码的适配(1)
- APP IOS6 适配iOS7开发计巧
- iOS6和iOS7代码的适配(4)——tableView
- (ios7) 解决Ios7中,Navigatebar 显示在主View中,和ios6 不一致问题
- IOS6和IOS7的屏幕适配问题