码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发——UI篇OC篇&TextField作为搜索框的使用

时间:2015-06-19 20:19:32      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

TextField作为搜索框的使用

 

在iOS开发中我们经常会使用到搜索框,但是有的时候系统自带的搜索框不足以满足我吗想要的功能,这个时候我们就可以使用自定义的搜索框实现想要的功能。

今天就简单的介绍一下怎么去使用UITextField实现一个搜索框,当然如果你有更好的方法也可以分享出来,大家相互学习。

一:直接使用

 1 UITextField *text = [[UITextField alloc] init];
 2     text.frame = CGRectMake(0, 0, 320, 30);
 3     text.background = [UIImage resizeImage:@"searchbar_textfield_background"];
 4     //文字垂直居中
 5     text.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
 6     //搜索图标
 7     UIImageView *view = [[UIImageView alloc] init];
 8     view.image = [UIImage resizeImage:@"searchbar_textfield_search_icon"];
 9     view.frame = CGRectMake(0, 0, 35, 35);
10     //左边搜索图标的模式
11     view.contentMode = UIViewContentModeCenter;
12     text.leftView = view;
13     //左边搜索图标总是显示
14     text.leftViewMode = UITextFieldViewModeAlways;
15     //右边删除所有图标
16     text.clearButtonMode = UITextFieldViewModeAlways;
17     
18     self.navigationItem.titleView = text;

二:封装

 1  //初始化
 2         /**
 3          *  使用TextField实现搜索框功能封装
 4          */
 5         //设置字体大小
 6         self.font = [UIFont systemFontOfSize:15];
 7         //设置提示文字
 8         self.placeholder = @"请输入搜索条件";
 9         //设置文本框的背景图片(使用分类实现拉伸效果)
10         self.background = [UIImage resizeImage:@"searchbar_textfield_background"];
11         //文字垂直居中
12         self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
13         //搜索图标
14         UIImageView *view = [[UIImageView alloc] init];
15         //设置图片控制器的的图片
16         view.image = [UIImage resizeImage:@"searchbar_textfield_search_icon"];
17         //设置图片的frame
18         view.width = 30;
19         view.height = 30;
20         //左边搜索图标的模式
21         view.contentMode = UIViewContentModeCenter;
22         self.leftView = view;
23         //左边搜索图标总是显示
24         self.leftViewMode = UITextFieldViewModeAlways;
25         //右边删除所有图标
26         self.clearButtonMode = UITextFieldViewModeAlways;

 



    /**使用一:
     *  使用封装好的SearchBar
     */

1     iCocosSearchBar *searchbar = [iCocosSearchBar searchBar];
2     searchbar.width = 300;
3     searchbar.height = 30;
4     self.navigationItem.titleView = searchbar;

 


    
    
    /**使用二
     *  使用封装好的SearchBar
     */
 

1    iCocosSearchBar *searchbar = [iCocosSearchBar searchBar];
2     searchbar.width = 300;
3     searchbar.height = 30;
4     [self.view addSubview:searchbar];

 



 

iOS开发——UI篇OC篇&TextField作为搜索框的使用

标签:

原文地址:http://www.cnblogs.com/iCocos/p/4589610.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!