修改UISearchBar背景
UISearchBar是由两个subView组成的,一个是UISearchBarBackGround,另一个是UITextField. 而IB中没有直接操作背景的属性,在此我总结了几个方法去修改它。
1. 只显示UITextField.采用了layer mask.代码如下:
- //first make sure you include core animation so that the compiler will know about your view's layer
- #import <QuartzCore/QuartzCore.h>
- //now make a mask. this is basically just a solid colored shape. When you apply the mask, anywhere where the color is solid will become transparent in your view. i used the excellent Opacity (http://likethought.com/opacity/) to generate this code, but you can do it any way you'd like
- @interface SearchMaskLayer : CALayer {
- }
- @end
- @implementation SearchMaskLayer
- - (void)drawInContext:(CGContextRef)context
- {
- CGRect imageBounds = CGRectMake(0, 0, 310, 34);
- CGRect bounds = imageBounds;
- CGFloat alignStroke;
- CGFloat resolution;
- CGMutablePathRef path;
- CGPoint point;
- CGPoint controlPoint1;
- CGPoint controlPoint2;
- UIColor *color;
- resolution = 0.5 * (bounds.size.width / imageBounds.size.width + bounds.size.height / imageBounds.size.height);
- CGContextSaveGState(context);
- CGContextTranslateCTM(context, bounds.origin.x, bounds.origin.y);
- CGContextScaleCTM(context, (bounds.size.width / imageBounds.size.width), (bounds.size.height / imageBounds.size.height));
- // Layer 1
- alignStroke = 0.0;
- path = CGPathCreateMutable();
- point = CGPointMake(295.0, 32.0);
- point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution;
- point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution;
- CGPathMoveToPoint(path, NULL, point.x, point.y);
- point = CGPointMake(310.0, 17.0);
- point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution;
- point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution;
- controlPoint1 = CGPointMake(303.229, 32.0);
- controlPoint1.x = (round(resolution * controlPoint1.x + alignStroke) - alignStroke) / resolution;
- controlPoint1.y = (round(resolution * controlPoint1.y + alignStroke) - alignStroke) / resolution;
- controlPoint2 = CGPointMake(310.0, 25.229);
- controlPoint2.x = (round(resolution * controlPoint2.x + alignStroke) - alignStroke) / resolution;
- controlPoint2.y = (round(resolution * controlPoint2.y + alignStroke) - alignStroke) / resolution;
- CGPathAddCurveToPoint(path, NULL, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, point.x, point.y);
- point = CGPointMake(310.0, 17.0);
- point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution;
- point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution;
- CGPathAddLineToPoint(path, NULL, point.x, point.y);
- point = CGPointMake(295.0, 2.0);
- point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution;
- point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution;
- controlPoint1 = CGPointMake(310.0, 8.771);
- controlPoint1.x = (round(resolution * controlPoint1.x + alignStroke) - alignStroke) / resolution;
- controlPoint1.y = (round(resolution * controlPoint1.y + alignStroke) - alignStroke) / resolution;
- controlPoint2 = CGPointMake(303.229, 2.0);
- controlPoint2.x = (round(resolution * controlPoint2.x + alignStroke) - alignStroke) / resolution;
- controlPoint2.y = (round(resolution * controlPoint2.y + alignStroke) - alignStroke) / resolution;
- CGPathAddCurveToPoint(path, NULL, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, point.x, point.y);
- point = CGPointMake(15.0, 2.0);
- point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution;
- point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution;
- CGPathAddLineToPoint(path, NULL, point.x, point.y);
- point = CGPointMake(0.0, 17.0);
- point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution;
- point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution;
- controlPoint1 = CGPointMake(6.771, 2.0);
- controlPoint1.x = (round(resolution * controlPoint1.x + alignStroke) - alignStroke) / resolution;
- controlPoint1.y = (round(resolution * controlPoint1.y + alignStroke) - alignStroke) / resolution;
- controlPoint2 = CGPointMake(0.0, 8.771);
- controlPoint2.x = (round(resolution * controlPoint2.x + alignStroke) - alignStroke) / resolution;
- controlPoint2.y = (round(resolution * controlPoint2.y + alignStroke) - alignStroke) / resolution;
- CGPathAddCurveToPoint(path, NULL, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, point.x, point.y);
- point = CGPointMake(0.0, 17.0);
- point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution;
- point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution;
- CGPathAddLineToPoint(path, NULL, point.x, point.y);
- point = CGPointMake(15.0, 32.0);
- point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution;
- point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution;
- controlPoint1 = CGPointMake(0.0, 25.229);
- controlPoint1.x = (round(resolution * controlPoint1.x + alignStroke) - alignStroke) / resolution;
- controlPoint1.y = (round(resolution * controlPoint1.y + alignStroke) - alignStroke) / resolution;
- controlPoint2 = CGPointMake(6.771, 32.0);
- controlPoint2.x = (round(resolution * controlPoint2.x + alignStroke) - alignStroke) / resolution;
- controlPoint2.y = (round(resolution * controlPoint2.y + alignStroke) - alignStroke) / resolution;
- CGPathAddCurveToPoint(path, NULL, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, point.x, point.y);
- point = CGPointMake(295.0, 32.0);
- point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution;
- point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution;
- CGPathAddLineToPoint(path, NULL, point.x, point.y);
- CGPathCloseSubpath(path);
- color = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0];
- [color setFill];
- CGContextAddPath(context, path);
- CGContextFillPath(context);
- CGPathRelease(path);
- }
- 然后在你的controller中应用这个mask layer到你的UISearchBar
- - (void)viewDidLoad {
- [super viewDidLoad];
- SearchMaskLayer *maskLayer = [[SearchMaskLayer alloc] init];
- [maskLayer setFrame:CGRectMake(0, 0, 310, 34)];
- [maskLayer setPosition:CGPointMake(162,21)];
- [maskLayer setNeedsDisplay];
- [self.searchBar.layer setNeedsDisplay];
- [self.searchBar.layer setMask:maskLayer];
- [maskLayer release];
- }
2. 隐藏背景。非官方的方法。
- for (UIView *subview in searchBar.subviews) {
- if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
- [subview removeFromSuperview];
- break;
- }
- }
3. 改变UISearchBar外观。 你可以子类化或category UISearchBar,然后实现两个方法,见代码。
- - (void)drawRect:(CGRect)rect {
- // UIImage *image = [UIImage imageNamed: @"background.png"];
- // [image drawInRect:rect];
- }
- - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
- UIImage *img = [UIImage imageNamed: @"background.png"];
- UIImageView *v = [[[UIImageView alloc] initWithFrame:CGRectZero] autorelease];
- [v setImage:img];
- v.bounds = CGRectMake(0, 0, img.size.width, img.size.height);
- NSLog([NSString stringWithFormat:@"%f:%f",img.size.width, img.size.height]);
- NSArray *subs = self.subviews;
- for (int i = 0; i < [subs count]; i++) {
- id subv = [self.subviews objectAtIndex:i];
- if ([subv isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
- {
- CGRect viewRect = [subv frame];
- [v setFrame:viewRect];
- [self insertSubview:v atIndex:i];
- }
- }
- [v setNeedsDisplay];
- [v setNeedsLayout];
- }
UISearchBar也是一个UIView,所以你可以像对待UIView一样对待它。
修改UISearchBar背景的更多相关文章
- 修改UISearchBar背景颜色
UISearchBar是由两个subView组成的,一个是UISearchBarBackGround,另一个是UITextField. 要IB中没有直接操作背景的属性.方法一:是直接将 UISearc ...
- 修改 UISearchBar cancelButton 样式
今天收到个问题,老大让我修改UISearchBar cancelButton的样式本来以为很简单的一个活,没想到让我长知识了. 开始在网上搜到的方法和我想象的一样,通过遍历Subviews获得butt ...
- jquery入门 修改网页背景颜色
我们在浏览一些网站,尤其是一些小说网站的时候,都会有修改页面背景颜色的地方,这个功能使用jquery很容易实现. 效果图: show you code: <!doctype html> & ...
- 修改UISearchBar的背景颜色
当你看到这篇博客你就已经发现了用_searchBar.backgroundColor = [UIColor clearColor];来设置UISearchBar的颜色完全没有效果: 并且,有些方法是想 ...
- ubuntu修改grub背景
grub背景由/etc/grub.d/05_debian_theme定义,修改图片只需要将图片文件放到/boot/grub,d/下即可, 修改颜色只需编辑/boot/grub.d/grub.cfg
- Confluence 6 CSS 指南:修改顶部背景
Confluence 默认页面的顶部是有关站点的菜单连接,在这里定义了 快速连接, 浏览菜单,用户菜单和快速查找输入框.在这个示例中,我们将会尝试修改顶部的菜单部分的背景和一些自定义的图片. 创建一个 ...
- android 开发 修改系统背景(状态栏颜色、导航栏颜色、标题栏颜色等等)
1.打开values下的styles.xml 发现有以下代码: <resources> <!-- Base application theme. --> <style n ...
- 【BIRT】修改主题背景颜色
下图是BIRT默认的颜色配置,为了跟系统颜色格局相一致,此处需要对颜色进行修改; 下面简单介绍了如何修改不同位置的背景颜色 对应文件地址均在目录:../webcontent/birt/styles下 ...
- 【VS开发】修改窗口背景颜色大全
如何修改frame窗口的背景颜色? MDI窗口的客户区是由frame窗口拥有的另一个窗口覆盖的.为了改变frame窗口背景的颜色,只需要这个客户区的背景颜色就可以了.你必须自己处理WM_ERASEB ...
随机推荐
- python无法启动火狐浏览器且报错“selenium.common.exceptions.WebDriverException: Message: Unable to find a matching set of capabilities”
安装了python2,使用pip安装了selenium,但是在使用时,报了“selenium.common.exceptions.WebDriverException: Message: 'gecko ...
- 2018-8-10-如何使用-C#-爬虫获得专栏博客更新排行
title author date CreateTime categories 如何使用 C# 爬虫获得专栏博客更新排行 lindexi 2018-08-10 19:16:51 +0800 2018- ...
- TSQL:让监控分析更简单更高效
1. 前言 阿里时序时空数据库TSDB最新推出TSQL,支持标准SQL的语法和函数.用户使用熟悉的SQL,不仅仅查询更简单易用,用户还可以利用SQL强大的功能,实现更加复杂的计算分析. 2. 为什么需 ...
- 【Python之路22】冒泡排序算法
1.变量互换 a = 123 b = 456 temp = a a = b b = temp python比较简单的变量互换: a = 123 b = 456 a,b = b,a print(a,b) ...
- MySQL与PostgreSQL比较 哪个数据库更好
最后结论说的好,通常由团队成员的熟悉度来决定: PostgreSQL 的名字很少听到,最近试装发现不是很友好:官方文档写的对新手来说有点坑: 有数据库工作经验的直接看最后一句就可以. 如果打算为项目选 ...
- 【JZOJ3624】【SDOI2014】数数(count) AC自动机+数位dp
题面 100 容易想到使用AC自动机来处理禁忌子串的问题: 然后在自动机上数位dp,具体是: \(f_{i,j,0/1}\)表示填了\(i\)位,当前在自动机的第\(j\)个结点上,\(0\)表示当前 ...
- Linux下下载安装jdk1.7和IDEA
一.安装JDK1.7 准备: 到Oracle官网下载下载jdk1.7,参考博客 https://blog.csdn.net/H12KJGJ/article/details/79240984 官网地址: ...
- jQuery 图片上传
1. 概述 1.1 说明 在一些前后端不分离的项目中,经常有一些需要把文件或者图片上传的功能,故记录此代码以便后期使用. 1.2 要求 1.上传,10M以内,限bmp,jpg,png,jpeg等图片格 ...
- tablespaces
select * from user_tablespaces; select username,default_tablespace from user_users;
- SpringBoot Actuator监控【转】
springboot actuator 监控 springboot1.5和springboot2.0 的actuator在启动日志上的差异就很大了. springboot1.5在启动时会打印很多/XX ...