使用UITextField自定义UISearchBar
2016-07-29 20:28
447 查看
使用UITextField自定义UISearchBar
字数267阅读51 评论1 喜欢1
有的时候,使用iOS自带的控件不符合变态产品狗的需求时,我们只好自定义控件来满足他们。在此处,我们通过创建一个GUSearchBar类,继承自UITextField类来自定义搜索框。
创建GUSearchBar
代码如下:GUSearchBar.h#import <UIKit/UIKit.h> @interface GUSearchBar : UITextField +(instancetype) searchBar; @end
GUSearchBar.m
#import "GUSearchBar.h" @implementation GUSearchBar -(id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.background = [UIImage resizedImage:@"searchbar_textfield_background"]; self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; UIImageView *leftView = [[UIImageView alloc] init]; leftView.image = [UIImage imageWithName:@"searchbar_textfield_search_icon"]; leftView.width = leftView.image.size.width + 10; leftView.height = leftView.image.size.height; leftView.contentMode = UIViewContentModeCenter; self.leftView = leftView; self.leftViewMode = UITextFieldViewModeAlways; self.clearButtonMode = UITextFieldViewModeAlways; self.placeholder = @"搜索"; } return self; } +(instancetype) searchBar { return [[self alloc] init]; } @end
注意:上述有类似leftView.width,leftView.height这样的语法是使用了自定义UIView的扩展分类,详情请点击UIView扩展。
使用
GUSearchBar *searchBar = [[GUSearchBar alloc] init]; searchBar.width = 350; searchBar.height = 30; self.navigationItem.titleView = searchBar;
使用效果
GUSearchBar
来自RecluseGu的博客:http://reclusegu.github.io/2016/06/29/Customizing-UISearchBar-with-UITextField/
相关文章推荐
- iOS常用公共方法
- ios学习资料
- 关于iOS常用的26中公共方法,可copy的代码
- iOS开发经验相关知识
- iOS开发之控件带箭头的弹出窗
- iOS 面试题~愿所有移动开发者熬过寒冬
- 来来来!关于iOS基础面试咱俩好好唠唠
- iOS中字迹动画效果
- iOS开发插件推荐(1):利用神插件injectionforxcode倍增你的开发效率
- iOS应用支持IPV6,就那点事儿
- YYWebImage工作原理介绍-----下载单张图片
- iOS常用公共方法
- iOS中字符串的用法,看这里就够了(详细)
- iOS-TabBar中间凸起点击完全有反应
- iOS长按图片保存实现方法
- iOS开发 给Label加下划线、中划线
- 谈谈iOS面试常提及到的线程间的通信
- iOS开发 - 清除缓存
- git与github之降龙十八掌
- iOS常用方法整理超全