首先通过 KVC 获取到内部的 textField, 然后自定制处理

UITextField *searchField = [searchBar valueForKey:@"searchField"];

if (searchField) {

[searchField setBorderStyle:UITextBorderStyleNone];

//placeHolder颜色

[searchField setValue:_placeholderColor forKeyPath:@"_placeholderLabel.textColor"];

searchField.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);

searchField.textColor = _textColor;

//字体大小

[searchField setValue:_placeHolderFont forKeyPath:@"_placeholderLabel.font"];

searchField.font = _textFont;

UIImage *image = _leftImage;

UIImageView *leftImg = [[UIImageView alloc] initWithImage:image];

leftImg.frame = CGRectMake(0,0,image.size.width, image.size.height);

searchField.leftView = leftImg;

}

其他: 添加圆角(放在一个有圆角的 bgView上面)

UIView *bg = [[UIView alloc]initWithFrame:CGRectMake(20, 220, 300, 30)];

bg.backgroundColor = [UIColor whiteColor];

[self.view addSubview:bg];

bg.layer.cornerRadius = 15;

bg.layer.masksToBounds = YES;

UISearchBar *bar = [[UISearchBar alloc]initWithFrame: bg.bounds];

[bg addSubview:bar];

bar.placeholder = @"搜索...";

[bar setBackgroundImage:[UIImage imageNamed:@"clearImage@2x"]];

修改圆角大小

首先在 UIView 的 category 里加一个方法:
- (UIView*)subViewOfClassName:(NSString*)className {
for (UIView* subView in self.subviews) {
if ([NSStringFromClass(subView.class) isEqualToString:className]) {
return subView;
} UIView* resultFound = [subView subViewOfClassName:className];
if (resultFound) {
return resultFound;
}
}
return nil;
} 用的时候:
UIView* backgroundView = [searchBar subViewOfClassName:@"_UISearchBarSearchFieldBackgroundView"];
backgroundView.layer.cornerRadius = 14.0f;
backgroundView.clipsToBounds = YES;
用这个方法还可以改取消按钮的颜色、字体什么的。

UISearchBar 自定义处理的更多相关文章

  1. IOS开发之UISearchBar自定义外观

      MySearchBar.h如下: @interface MySearchBar : UISearchBar - (void)layoutSubviews; @end MySearchBar.m如下 ...

  2. UIsearchBar 自定义样式

    对于修改输入框圆角 的需求, 可以通过修改搜索框背景图片的方式设置. - (void)setSearchFieldBackgroundImage:(nullable UIImage *)backgro ...

  3. ios学习流水账2

    1.UISearchBar自定义背景.取消按钮中文设置 UISearchBar *seachBar=[[UISearchBar alloc] init]; //修改搜索框背景 seachBar.bac ...

  4. iOS 如何自定义UISearchBar 中textField的高度

    iOS 如何自定义UISearchBar 中textField的高度 只需设置下边的方法就可以 [_searchBar setSearchFieldBackgroundImage:[UIImage i ...

  5. 新浪微博客户端(5)-自定义UISearchBar

    iOS自带的UISearchBar有很多限制,我们可以使用UITextField做出一个类似于SearchBar的效果. //===================================== ...

  6. iOS开发UI篇 -- UISearchBar 属性、方法详解及应用(自定义搜索框样式)

    很多APP都会涉及到搜索框,苹果也为我们提供了默认的搜索框UISearchBar.但实际项目中我们通常需要更改系统默认搜索框的样式.为了实现这一目标,我们需要先搞懂 UISearchBar 的属性及方 ...

  7. 自定义UISearchBar

    先上系统默认的UISearchBar,然后用KVO修改 UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:_topView.boun ...

  8. 自定义UISearchBar外观

    1. 设置背景色 我以白色的背景色为例,下面看看代码: //1. 设置背景颜色 //设置背景图是为了去掉上下黑线 self.customSearchBar.backgroundImage = [[UI ...

  9. iOS 将navigationItem.titleView设置为自定义UISearchBar (Ficow实例讲解)

    这篇文章可以解决以下问题: 1.将searchBar设置为titleView后,无法调整位置的问题 : 2.searchBar的背景色无法设置为透明色的问题: 3.searchBar输入框内用户输入的 ...

随机推荐

  1. 设备控制接口ioctl详解

    [转]Linux设备控制接口 序言设备驱动程序的一个基本功能就是管理和控制设备,同时为用户应用程序提供管理和控制设备的接口.我们前面的“Hello World”驱动程序已经可以提供读写功能了,在这里我 ...

  2. iPython notebook 安装使用

    pip install jupyter jupyter notebook --allow-root

  3. 使用Apache IO库操作IO与文件

    --------------siwuxie095                         首先到 Apache官网 下载相关的库文件     Apache官网:http://www.apach ...

  4. 安装 SQL Server 2014 Express

    安装 SQL Server 2014 Express 我的电脑系统: Windows 10 64位 一 . 下载 安装Microsoft SQL Server 2014 Express 软甲下载地址: ...

  5. 在Linux中监视IO性能

    dd命令 iostat命令 理解iostat的各项输出 iostat的应用实例 附:在Windows中监视IO性能 延伸阅读 dd命令 dd其实是工作于比较低层的一个数据拷贝和转换的*nix平台的工具 ...

  6. MySQL——explain性能分析的使用

    用法:explain sql语句: id:查询的序号. ref:进行连接查询时,表得连接关系.可以通过上图看出. select_type:select查询的类型,主要是区别普通查询和联合查询.子查询之 ...

  7. 第一章Python简介

    1.Python shell(Python命令解释器) 如下所示 2.Python的交互模式 如下 3.代码编辑器窗口 在上面的那些指令称为源代码. 4.在python中,缩进是有语法意义的. 在某行 ...

  8. AutoLayout自动布局,NSLayoutConstraint 视图约束使用

    一.方法 NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:<#(id)#> attribut ...

  9. git手动解决内容冲突

    <span style="font-size:18px;">git checkout -b lab4 origin/lab4 git merge lab3</sp ...

  10. 状态压缩DP----HDU2809

    状态压缩DP的一道较不错的入门题,第二次做这类问题,感觉不是很顺手,故记录下来. 题目的意思就是吕布战群雄,先给你6个数,分别是吕布的攻击值,防御值,生命值,升级后此三值各自的增量,然后是对手的个数n ...