自定义UISearchBar 适配IOS6和IOS7
来源:互联网 发布:施工计划横道图软件 编辑:程序博客网 时间:2024/04/30 13:20
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]; } 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
0 0
- 自定义UISearchBar 适配IOS6和IOS7
- 自定义UISearchBar 适配IOS6和IOS7
- 自定义UISearchBar 适配IOS6和IOS7 修改放大镜图标 修改光标颜色 修改边框颜色 placeholder颜色 设置文本框背景
- 自定义UISearchBar 适配IOS6和IOS7 修改放大镜图标 修改光标颜色 修改边框颜色 placeholder颜色 设置文本框背景
- iOS7和iOS6适配
- 适配ios6和ios7
- iOS7和iOS6适配 q
- UITableView和UISearchBar在ios7和ios6之间的区别【转】
- UITableView和UISearchBar在ios7和ios6之间的区别【转】
- 适配iOS7之—UITableView和UISearchBar
- 适配iOS7之—UITableView和UISearchBar
- 适配iOS7之—UITableView和UISearchBar
- 关于UISearchBar在IOS6和IOS7的区别以及用法注意
- ios6,ios7,ios7.1下设置UISearchbar的背景色
- ios6,ios7,ios7.1下设置UISearchbar的背景色
- ios6,ios7,ios7.1下设置UISearchbar的背景色
- ios6,ios7,ios7.1下设置UISearchbar的背景色
- ios6,ios7,ios7.1下设置UISearchbar的背景色
- Gallery无限循环
- 如何编出健壮的代码 java编程30条规则
- java 位运算详解
- 64位SQLSERVER导入EXCEL表
- oj之路(第二天)(由题目看思想)
- 自定义UISearchBar 适配IOS6和IOS7
- Drawing Text(一)绘制文本
- django+python开发的网站怎么静态引用img图片
- [Python] 实现网络爬虫
- 解决TOMCAT支持J2EE版本问题
- Openfire之Spark开发环境搭建
- OpenGL入门示例4——黑色背景绘制彩色点画线
- c++操作mysql - 连接
- vim 中文乱码