UI控件笔记(十五):UI之自定义搜索框的封装
来源:互联网 发布:淘宝达人账号简介内容 编辑:程序博客网 时间:2024/06/05 19:55
一、SearchBar.h文件
#import <UIKit/UIKit.h>
@protocol SearchBarDelegate <NSObject>//要把当前搜索View里的搜索内容传给VC
-(void)sendTextToVC:(NSString*)text;
@end
@interface SearchBar : UIView<UITextFieldDelegate>
@property(nonatomic,assign)id<SearchBarDelegate>delegate;
@end
二、SearchBar.m文件
#import "SearchBar.h"
#import <QuartzCore/QuartzCore.h>
@implementation SearchBar
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
self.backgroundColor = [UIColor colorWithRed:222/255.0 green:222/255.0 blue:222/255.0 alpha:1];
UITextField *searchBar = [[UITextField alloc] initWithFrame:CGRectMake(10,7,300,30)];
//把一个UI变成圆角的,ios6之前需要添加QuartzCore.framework库
searchBar.layer.masksToBounds = YES;//当前V上面的子视图不会超了
searchBar.layer.cornerRadius = 5;
searchBar.backgroundColor = [UIColor whiteColor];
searchBar.delegate = self;
searchBar.placeholder = @"60万款应用搜索看";
[self addSubview:searchBar];
[searchBar release];
searchBar.tag = 5000;
UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
cancelBtn.frame = CGRectMake(self.frame.size.width-70,7,60,30);
[cancelBtn setTitle:@"cancel" forState:UIControlStateNormal];
[cancelBtn addTarget:self action:@selector(cancelDown) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:cancelBtn];
cancelBtn.tag = 1999;
cancelBtn.hidden = YES;
}
returnself;
}
-(void)cancelDown//取消搜索功能
{
UIButton *cancelBtn = (UIButton*)[self viewWithTag:1999];
cancelBtn.hidden = YES;
UITextField *textField = (UITextField*)[self viewWithTag:5000];
[textField resignFirstResponder];
textField.text = @"";//取消的时候,文本输入框里置空
[UIView animateWithDuration:2 animations:^{
textField.frame = CGRectMake(10,7,300,30);
}];
[self.delegate sendTextToVC:@""];//取消的时候,给vc传一个空字符串,当vc收到空字符串的时候,表示不使用搜索接口,而使用当前VC正常的loadData接口(下拉刷新接口)
}
#pragma mark textfield代理
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[UIView animateWithDuration:2 animations:^{
textField.frame = CGRectMake(10,7,210,30);
} completion:^(BOOL finished) {
UIButton *cancelBtn = (UIButton*)[self viewWithTag:1999];
cancelBtn.hidden = NO;
}];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField//搜索
{
[textField resignFirstResponder];
[self.delegate sendTextToVC:textField.text];//代理传文本字符串
returnYES;
}
@end
- UI控件笔记(十五):UI之自定义搜索框的封装
- UI控件笔记(十四):UI之自定义导航条的封装
- UI控件笔记(十二):UI之TableEdit\ UISearchBar\自定义实时搜索\ 索引
- UI控件笔记(六):UI之UINavigationController、navigationBar和UIView的封装
- UI控件笔记(十六):UI之Uibutton,UIview,UIlbel等初始化的封装
- 自定义搜索框的UI
- UI控件笔记(一):UI之UIView的属性
- UI控件笔记(三):UI之UIButton的属性
- UI控件笔记(四):UI之UITextField的属性
- UI控件笔记(十一):UI之UITableView\自定义UITableView\分组
- UI控件笔记(二):UI之UILabel和UIImageView
- UI控件笔记(五):UI之UIViewController和NSUserDefaults
- UI控件笔记(九):UI之UITabBarController
- UI控件笔记(十三):UI之MVC
- UI控件笔记(十七):UI之UI阶段控件常用方法大全总结
- UI之搜索框的创建Search
- C#程序员整理的Unity 3D笔记(十五):Unity 3D UI控件至尊–NGUI
- UI基础学习之(十五):UIPickerView
- 解析src下的xml配置文件最简单的方法
- OpenCV中C++函数imread读取图片的缩放问题
- Python中str(x) 与``与repr(x)区别
- [ViewPager、二]ViewPager 小圆点 动态添加
- [转]MySQL存储过程调试工具(dbForge Studio for MySQL工具)
- UI控件笔记(十五):UI之自定义搜索框的封装
- Log4j输出到控制台顺利,写入文件失败 - Log4j和commons log的整合
- Spring AOP 方法内部不生效
- iOS系统下检查第三方APP是否安装及跳转启动
- iOS 点击图片放大效果
- C++多态:从虚表指针到设计模式
- 博客导航
- mysql存储过程之游标遍历数据表
- 这些年,遇到过的恼火的项目交接