UISearchBar 自定义处理
首先通过 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 自定义处理的更多相关文章
- IOS开发之UISearchBar自定义外观
MySearchBar.h如下: @interface MySearchBar : UISearchBar - (void)layoutSubviews; @end MySearchBar.m如下 ...
- UIsearchBar 自定义样式
对于修改输入框圆角 的需求, 可以通过修改搜索框背景图片的方式设置. - (void)setSearchFieldBackgroundImage:(nullable UIImage *)backgro ...
- ios学习流水账2
1.UISearchBar自定义背景.取消按钮中文设置 UISearchBar *seachBar=[[UISearchBar alloc] init]; //修改搜索框背景 seachBar.bac ...
- iOS 如何自定义UISearchBar 中textField的高度
iOS 如何自定义UISearchBar 中textField的高度 只需设置下边的方法就可以 [_searchBar setSearchFieldBackgroundImage:[UIImage i ...
- 新浪微博客户端(5)-自定义UISearchBar
iOS自带的UISearchBar有很多限制,我们可以使用UITextField做出一个类似于SearchBar的效果. //===================================== ...
- iOS开发UI篇 -- UISearchBar 属性、方法详解及应用(自定义搜索框样式)
很多APP都会涉及到搜索框,苹果也为我们提供了默认的搜索框UISearchBar.但实际项目中我们通常需要更改系统默认搜索框的样式.为了实现这一目标,我们需要先搞懂 UISearchBar 的属性及方 ...
- 自定义UISearchBar
先上系统默认的UISearchBar,然后用KVO修改 UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:_topView.boun ...
- 自定义UISearchBar外观
1. 设置背景色 我以白色的背景色为例,下面看看代码: //1. 设置背景颜色 //设置背景图是为了去掉上下黑线 self.customSearchBar.backgroundImage = [[UI ...
- iOS 将navigationItem.titleView设置为自定义UISearchBar (Ficow实例讲解)
这篇文章可以解决以下问题: 1.将searchBar设置为titleView后,无法调整位置的问题 : 2.searchBar的背景色无法设置为透明色的问题: 3.searchBar输入框内用户输入的 ...
随机推荐
- The centos disc was not found in any of your drives.Please insert the centos disc and press OK to retry
查看虚拟机设置中关于CDROM的选项,将CDROM的状态改为已连接,不要奇怪,勾选上之后再按下OK就好了
- sublime添加右键菜单
参考:https://www.zhihu.com/question/29662273/answer/45277925 @echo Off :START CLS echo *============== ...
- Game Develop Books
[Working On] [Pending] 3.<实时计算机图形学> 4.<游戏编程精粹1> 5.<游戏编程精粹2> 6.<3D游戏引擎设计:实时计算机图形 ...
- java基础之集合长度可变的实现原理
首先我们要明白java中的集合Collection,List,ArrayList之间的关系: ArrayList是具体的实现类,实现了List接口 List是接口,继承了Collection接口 Li ...
- 带你剖析WebGis的世界奥秘----Geojson数据加载(高级)(转)
带你剖析WebGis的世界奥秘----Geojson数据加载(高级) 转:https://zxhtom.oschina.io/zxh/20160819.html 编程 java 2016/08/ ...
- java代理模式与装饰者模式
静态代理和装饰者模式的区别: 先来看一下装饰者模式的定义:装饰者模式动态地将责任附加到对象上.若要扩展功能,装饰者提供了比继承更有弹性的替代方案. 总结一下采用装饰者模式是为了增强或拓展原对象的功能. ...
- IDEA02 利用Maven创建Web项目、为Web应用添加Spring框架支持、bean的创建于获取、利用注解配置Bean、自动装配Bean、MVC配置
1 环境版本说明 Jdk : 1.8 Maven : 3.5 IDEA : 专业版 2017.2 2 环境准备 2.1 Maven安装及其配置 2.2 Tomcat安装及其配置 3 详细步骤 3.1 ...
- docker问题:docker端口映射错误
1 docker端口映射错误 1.1 问题描述 利用docker启动nginx容器的时候报错: 1.2 解决办法 一次执行下面的命令就可以解决 pkill docker iptables -t nat ...
- Luogu 4721 【模板】分治 FFT
还不会这题的多项式求逆的算法. 发现每一项都是一个卷积的形式,那么我们可以使用$NTT$来加速,直接做是$O(n^2logn)$的,我们考虑如何加速转移. 可以采用$cdq$分治的思想,对于区间$[l ...
- css模块化思想(一)
什么是css模块化思想?(what) 为了理解css模块化思想,我们首先了解下,什么是模块化,在百度百科上的解释是,在系统的结构中,模块是可组合.分解和更换的单元.模块化是一种 处理复杂系统分解成为更 ...