封装到 HMSearchBar.h / .m

#import <UIKit/UIKit.h>

@interface HMSearchBar : UITextField
+ (instancetype)searchBar;
@end
#import "HMSearchBar.h"

@implementation HMSearchBar

- (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 + ;
leftView.height = leftView.image.size.height;
// 设置leftView的内容居中
leftView.contentMode = UIViewContentModeCenter;
self.leftView = leftView; // 设置左边的view永远显示
self.leftViewMode = UITextFieldViewModeAlways; // 设置右边永远显示清除按钮
self.clearButtonMode = UITextFieldViewModeAlways;
}
return self;
} + (instancetype)searchBar
{
return [[self alloc] init];
}
@end

控制器 调用 HMSearchBar

#import "HMDiscoverViewController.h"
#import "HMSearchBar.h" @interface HMDiscoverViewController () @end @implementation HMDiscoverViewController - (void)viewDidLoad
{
[super viewDidLoad]; HMSearchBar *searchBar = [HMSearchBar searchBar];
searchBar.width = ;
searchBar.height = ;
self.navigationItem.titleView = searchBar;
}

IOS 搜索条制作(UITextField 封装)的更多相关文章

  1. iOS 搜索条使用详解

    在ios开发中搜索条的使用挺常见的,不过之前一直没用到也没细细研究,最近做外包项目的时候刚好用到,在这里记录一下使用的过程,只要理解了原理,其实还是比较简单的!上传的图片有点大,刚好可以看清楚它的使用 ...

  2. 模仿京东顶部搜索条效果制作的一个小demo

    最近模仿京东顶部搜索条效果制作的一个小demo,特贴到这里,今后如果有用到可以参考一下,代码如下 #define kScreenWidth [UIScreen mainScreen].bounds.s ...

  3. Swift - 搜索条(UISearchBar)的用法

    1,搜索条Options属性还可设置如下功能样式: Shows Search Results Button:勾选后,搜索框右边显示一个圆形向下的按钮,单击会发送特殊事件. Shows Bookmark ...

  4. Unity跳转场景进度条制作教程(异步加载)

    Unity跳转场景进度条制作 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享 ...

  5. 三、jQuery--jQuery实践--搜索框制作

    input标签讲解 <input/>作为按钮的type属性:button.submit(后面会有二者对比分析)

  6. 使用CSS3和jQuery可伸缩的搜索条

    使用CSS3和jQuery可伸缩的搜索条 helloweba.com 作者:月光光 时间:2013-12-09 21:23 标签: CSS3 jquery 搜索条在我们网站是必不可少的,尤其是在有限的 ...

  7. 一个漂亮的DIV搜索条

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. Unity 3D 进度条制作

    我们都知道玩游戏时,第一步要加载游戏,加载游戏时我们可以做一个简单的进度条来显示游戏加载进度,应为有了进度条,游戏画面不会过于呆板. 那么我们就开始游戏的进度条制作吧! 方法一: 1,使用NGUI制作 ...

  9. Unity NGUI 血条制作

    NGUI 血条制作步骤 实现过程: 模拟血条的变化当点击按钮Button是血条会实时发生变化. 1.向Unity中导入NGUI2.6.3.unitypackage 点击create your ui 后 ...

随机推荐

  1. day18 约束 异常

    1. 类的约束 方案一:抛出异常,子类必须重写父类方法,否则会报错 class Base: def login(self): #强制xxx做XXX事 raise NotImplementedError ...

  2. 服务器查看外网IP地址和方法

    返回IP地址 curl ip..com/ip.aspx curl whatismyip.akamai.com wget -qO - ifconfig.co curl icanhazip.com dig ...

  3. 移动性能测试 | 持续集成中的 Android 稳定性测试

    前言 谈到Android稳定测试,大多数会联想到使用monkey工具来做测试.google官方提供了monkey工具,可以很快速点击被应用,之前我有一篇帖子提到了monkey工具的使用,详见: htt ...

  4. JavaScript中使用ActiveXObject操作本地文件夹的方法

    转载地址    http://www.jb51.net/article/48538.htm 在Windows平台上, js可以调用很多Windows提供的ActivexObject,本文就使用js来实 ...

  5. 问题:modbus_tk开发中遇到[Errno 98] Address already in use (已解决)

    案例: from modbus_tk import modbus_tcp,defines import time s = modbus_tcp.TcpServer(port=5300) def mai ...

  6. sf01_什么是数据结构

    数据结构解决什么问题 如何在计算机中存储数据和信息,采用什么样的方法和技巧加工处理这些数据,都是数据结构需要努力解决的问题. 解决问题的步骤 使用计算机解决问题的步骤:分析具体问题得到数学模型,设计解 ...

  7. Thinkphp3.2邮件发送

    第一步:加入这两个文件 第二部:在common的function中添加代码 function think_send_mail($to, $name, $subject = '', $body = '' ...

  8. Python Numpy Array

    Numpy 是Python中数据科学中的核心组件,它给我们提供了多维度高性能数组对象. Arrays Numpy.array   dtype 变量 dtype变量,用来存放数据类型, 创建数组时可以同 ...

  9. component: resolve => require(['../pages/home.vue'], resolve)

    component: resolve => require(['../pages/home.vue'], resolve) vue 路由的懒加载 import Vue from 'vue' im ...

  10. SQL datetime和smalldatetime区别

    datetime 存储大小8个字节,精确到分后的3为小数,日期范围从1753 年 1 月 1 日到 9999 年 12 月 31 日:而 smalldatetime存储大小为4个字节,精确到分,日期范 ...