首先通过 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. sftp put权限不够

    报错如下: sftp> put play.zip ./ Uploading play.zip to /opt/library/./play.zip remote open("/opt/ ...

  2. 解决nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)

    nginx先监听了ipv4的80端口之后又监听了ipv6的80端口,于是就重复占用了.更加坑人的是你去看了端口占用它又把80端口释放了,是不是很囧. 解决方案是编辑nginx的配置文件 修改这一段:

  3. solidity错误处理

    官方文档: https://solidity.readthedocs.io/en/develop/control-structures.html#error-handling-assert-requi ...

  4. CAP理论与HBase

    The short summary of the article is that CAP isn't "C, A, or P, choose two," but rather &q ...

  5. openpyxl模块处理excel文件

    python模块之——openpyxl 处理xlsx/ xlsm文件 项目原因需要编辑excel文件,经过查询,最先尝试xlwt .wlrd这个两个模块,但是很快发现这两个模块只能编辑xls文件,然而 ...

  6. 2.Hive的几种常见的数据导入方式

    好久没写Hive的那些事了,今天开始写点吧.今天的话题是总结Hive的几种常见的数据导入方式,我总结为四种:(1).从本地文件系统中导入数据到Hive表:(2).从HDFS上导入数据到Hive表:(3 ...

  7. Mybatis——缓存机制

    MyBatis 包含一个非常强大的查询缓存特性,它可以非常方便地配置和定制.缓存可以极大的提升查询效率. MyBatis系统中默认定义了两级缓存. 一级缓存和二级缓存. 1.默认情况下,只有一级缓存( ...

  8. Linux下面rpm命令和mount命令详解

    在Linux下面我们经常会安装一些软件包,还有挂载命令.接下来,我们通过一些实例来演示这些命令的使用.. 第一步:我们先在linux下面挂载光盘,先进入到根目录,然后切换到根下面的/mnt目录,因为/ ...

  9. HTTP协议相关介绍

    一.HTTP请求方法 根据HTTP标准,HTTP请求可以使用多种请求方法.HTTP1.0定义了三种请求方法: GET, POST 和 HEAD方法.HTTP1.1新增了五种请求方法:OPTIONS, ...

  10. 【Web API2】ASP.NET Web API Security

    实现安全的方式既可以是host提供,也可以框架提供. 1,HTTP Module 方式,工作在IIS上,所以web api要托管在IIS上才行.其作用于HTTP管道的最前端,所以这种方式影响的是全局, ...