| 
TA的每日心情|  | 汗 2024-10-15 10:05
 | 
|---|
 签到天数: 372 天 [LV.9]以坛为家II | 
 
| 考虑到统一ios6与ios7的界面风格,UISearchBar通过重自定义来实现,重写一个类继承自UISearchBar 1、效果图:
 
   2、部分代码:
 附注释说明
 SASearchBar.h
 SASearchBar.m复制代码#import <UIKit/UIKit.h>@interface SASearchBar : UISearchBar@property (nonatomic, strong)   UITextField     *textField;@property (nonatomic, strong)   UIButton        *searchBtn;@end
3、素材下载:复制代码#import "SASearchBar.h"@implementation SASearchBar#pragma mark 初始化- (id)initWithFrame:(CGRect)frame{    if (self = [super initWithFrame:frame]) {        // 设置背景色为透明,这句在ios6中很重要!!        self.backgroundColor = [UIColor clearColor];        self.placeholder = @"搜索号码或姓名";    }    return self;}#pragma mark 调整子控件- (void)layoutSubviews{    [super layoutSubviews];        UIView *baseView = iOS7 ? [self.subviews firstObject] : self;        // 移除背景图片    [[baseView.subviews firstObject] removeFromSuperview];        // 获取TextField    self.textField = [baseView.subviews firstObject];    self.textField.leftView = nil;    self.textField.background = nil;    self.textField.backgroundColor = [UIColor clearColor];    self.textField.frame = CGRectMake(0, 0, baseView.frame.size.width - 30, 44);        // 创建搜索按钮    self.searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];    UIImage *searchIcn = [UIImage imageNamed:@"icon_invite_search_btn.png"];    [self.searchBtn setImage:searchIcn forState:UIControlStateNormal];    self.searchBtn.frame = CGRectMake(baseView.frame.size.width - 30, 10, 20, 20);    [baseView addSubview:self.searchBtn];        NSLog(@"%@", self.subviews);}#pragma mark 绘制背景图片- (void)drawRect:(CGRect)rect{    UIImage *image = [UIImage resizeImage:@"textField_line_selected.png"];    [image drawInRect:CGRectMake(0, 0, rect.size.width, rect.size.height)];}@end
 Resource.zip
(18.54 KB, 下载次数: 4) | 
 |