自定义UISearchBar 适配IOS6和IOS7
考虑到统一ios6与ios7的界面风格,UISearchBar通过重自定义来实现,重写一个类继承自UISearchBar1、效果图:
2、部分代码:
附注释说明
SASearchBar.h
#import <UIKit/UIKit.h>
@interface SASearchBar : UISearchBar
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UIButton *searchBtn;
@endSASearchBar.m#import "SASearchBar.h"
@implementation SASearchBar
#pragma mark 初始化
- (id)initWithFrame:(CGRect)frame
{
if (self = ) {
// 设置背景色为透明,这句在ios6中很重要!!
self.backgroundColor = ;
self.placeholder = @"搜索号码或姓名";
}
return self;
}
#pragma mark 调整子控件
- (void)layoutSubviews
{
;
UIView *baseView = iOS7 ? : self;
// 移除背景图片
[ removeFromSuperview];
// 获取TextField
self.textField = ;
self.textField.leftView = nil;
self.textField.background = nil;
self.textField.backgroundColor = ;
self.textField.frame = CGRectMake(0, 0, baseView.frame.size.width - 30, 44);
// 创建搜索按钮
self.searchBtn = ;
UIImage *searchIcn = ;
;
self.searchBtn.frame = CGRectMake(baseView.frame.size.width - 30, 10, 20, 20);
;
NSLog(@"%@", self.subviews);
}
#pragma mark 绘制背景图片
- (void)drawRect:(CGRect)rect
{
UIImage *image = ;
;
}
@end3、素材下载:
页:
[1]