修改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一样对待它。
转载于:https://www.cnblogs.com/zsw-1993/p/4880003.html
修改UISearchBar背景相关推荐
- jquery入门 修改网页背景颜色
我们在浏览一些网站,尤其是一些小说网站的时候,都会有修改页面背景颜色的地方,这个功能使用jquery很容易实现. 效果图: show you code: <!doctype html> & ...
- RK3399 Ubuntu修改任务栏为自动隐藏和修改桌面背景
挂载文件系统 源码解读: OK3399-desktop-release //飞凌释放的Ubuntu系统源码 OK3399-desktop- ...
- intellij2018修改代码背景颜色
intellij中修改代码背景颜色 ,点击右侧background,如下图,颜色填入000000
- JQuery修改background-image背景图片
JQuery修改background-image背景图片 使用: var src = ' ../resource/images/top-img.png'; $("#videoCoverLay ...
- Visual Studio - 修改主题背景颜色
Visual Studio - 修改主题背景颜色 1 Dark 2 Tools -> Options 3 Environment -> General 4 Light 5 Blue Ref ...
- Grafana修改主题背景升级版
Grafana 主题介绍 Grafana中有一个Panel.这个Panel是Boom Theme自带三个主题.这三个主题分别是Night Theme.Dark Theme.Light Theme. N ...
- 去除影像黑边-修改影像背景值-比Envi影像去除黑边-ArcGIS去除影像黑边-好用
目录 去除黑边 一.可视化显示时去除黑边 二.输出去除黑边的影像 每一景去除黑边分别输出-操作方法 多个影像去除黑边镶嵌输出-操作方法 软件下载地址: https://pan.baidu.com/s/ ...
- element修改el-table 表头的背景颜色横向渐变色 + 修改表头背景颜色
vue + element-ui 修改el-table 表头的背景颜色横向渐变色 + 修改表头背景颜色 表头背景颜色横向渐变效果图: 修改表头背景颜色和字体颜色效果图: 直接上代码: 修改表头的背景颜 ...
- android 仿qq聊天背景,安卓手机qq如何修改聊天背景
如何大多数的用户都是使用安卓手机,如果我们想要修改安卓手机qq的聊天背景,应该如何修改呢?下面就让学习啦小编告诉你安卓手机qq如何修改聊天背景,希望对大家有所帮助. 安卓手机qq修改聊天背景的方法 手 ...
最新文章
- 超级干货:3个性能监控和优化命令详解
- Yii学习笔记:利用setFlash和runController打造个性化的提示信息页面
- python使用elasticsearch维护数据_使用Python对ElasticSearch获取数据及操作
- B+树(加强版多路平衡查找树)
- 闽高校计算机二级c语言模拟器,闽高校计算机二级C语言模拟卷及答案.doc
- 高频面试题2:单例设计模式
- mysql8导入不同schme_新特性解读 | MySQL 8.0.22 任意格式数据导入
- RAC环境下管理OGG-HA
- java json web token_一分钟简单了解JSON Web Token
- Visio_Premium_project_vol版
- Flink 异步IO
- 诊断Oracle数据库Hanging问题
- 基于CNN的IMDB电影数据集文本分类
- 华为展望5G未来 安全问题成关键
- 2020 — 只争朝夕,不负韶华
- 量化7年1000万知与行,回测量化7年如果只每月定投5.4万到沪深300指数基金,能否实现7年1000万
- 2021新上传QQ透明头像成品+源码
- SpringUtil 工具类
- 【git】------git的基本命令 (此文章转载我的老师 Alley-巷子)
- 【毒鸡汤】基层管理如果没有这些心态,难!