转载:http://blog.csdn.net/favormm/archive/2010/11/30/6045463.aspx

UISearchBar是由两个subView组成的,一个是UISearchBarBackGround,另一个是UITextField. 而IB中没有直接操作背景的属性,在此我总结了几个方法去修改它。

1. 只显示UITextField.采用了layer mask.代码如下:

view plaincopy to clipboardprint?
  1. //first make sure you include core animation so that the compiler will know about your view's layer
  2. #import <QuartzCore/QuartzCore.h>
  3. //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
  4. @interface SearchMaskLayer : CALayer {
  5. }
  6. @end
  7. @implementation SearchMaskLayer
  8. - (void)drawInContext:(CGContextRef)context
  9. {
  10. CGRect imageBounds = CGRectMake(0, 0, 310, 34);
  11. CGRect bounds = imageBounds;
  12. CGFloat alignStroke;
  13. CGFloat resolution;
  14. CGMutablePathRef path;
  15. CGPoint point;
  16. CGPoint controlPoint1;
  17. CGPoint controlPoint2;
  18. UIColor *color;
  19. resolution = 0.5 * (bounds.size.width / imageBounds.size.width + bounds.size.height / imageBounds.size.height);
  20. CGContextSaveGState(context);
  21. CGContextTranslateCTM(context, bounds.origin.x, bounds.origin.y);
  22. CGContextScaleCTM(context, (bounds.size.width / imageBounds.size.width), (bounds.size.height / imageBounds.size.height));
  23. // Layer 1
  24. alignStroke = 0.0;
  25. path = CGPathCreateMutable();
  26. point = CGPointMake(295.0, 32.0);
  27. point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution;
  28. point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution;
  29. CGPathMoveToPoint(path, NULL, point.x, point.y);
  30. point = CGPointMake(310.0, 17.0);
  31. point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution;
  32. point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution;
  33. controlPoint1 = CGPointMake(303.229, 32.0);
  34. controlPoint1.x = (round(resolution * controlPoint1.x + alignStroke) - alignStroke) / resolution;
  35. controlPoint1.y = (round(resolution * controlPoint1.y + alignStroke) - alignStroke) / resolution;
  36. controlPoint2 = CGPointMake(310.0, 25.229);
  37. controlPoint2.x = (round(resolution * controlPoint2.x + alignStroke) - alignStroke) / resolution;
  38. controlPoint2.y = (round(resolution * controlPoint2.y + alignStroke) - alignStroke) / resolution;
  39. CGPathAddCurveToPoint(path, NULL, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, point.x, point.y);
  40. point = CGPointMake(310.0, 17.0);
  41. point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution;
  42. point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution;
  43. CGPathAddLineToPoint(path, NULL, point.x, point.y);
  44. point = CGPointMake(295.0, 2.0);
  45. point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution;
  46. point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution;
  47. controlPoint1 = CGPointMake(310.0, 8.771);
  48. controlPoint1.x = (round(resolution * controlPoint1.x + alignStroke) - alignStroke) / resolution;
  49. controlPoint1.y = (round(resolution * controlPoint1.y + alignStroke) - alignStroke) / resolution;
  50. controlPoint2 = CGPointMake(303.229, 2.0);
  51. controlPoint2.x = (round(resolution * controlPoint2.x + alignStroke) - alignStroke) / resolution;
  52. controlPoint2.y = (round(resolution * controlPoint2.y + alignStroke) - alignStroke) / resolution;
  53. CGPathAddCurveToPoint(path, NULL, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, point.x, point.y);
  54. point = CGPointMake(15.0, 2.0);
  55. point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution;
  56. point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution;
  57. CGPathAddLineToPoint(path, NULL, point.x, point.y);
  58. point = CGPointMake(0.0, 17.0);
  59. point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution;
  60. point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution;
  61. controlPoint1 = CGPointMake(6.771, 2.0);
  62. controlPoint1.x = (round(resolution * controlPoint1.x + alignStroke) - alignStroke) / resolution;
  63. controlPoint1.y = (round(resolution * controlPoint1.y + alignStroke) - alignStroke) / resolution;
  64. controlPoint2 = CGPointMake(0.0, 8.771);
  65. controlPoint2.x = (round(resolution * controlPoint2.x + alignStroke) - alignStroke) / resolution;
  66. controlPoint2.y = (round(resolution * controlPoint2.y + alignStroke) - alignStroke) / resolution;
  67. CGPathAddCurveToPoint(path, NULL, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, point.x, point.y);
  68. point = CGPointMake(0.0, 17.0);
  69. point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution;
  70. point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution;
  71. CGPathAddLineToPoint(path, NULL, point.x, point.y);
  72. point = CGPointMake(15.0, 32.0);
  73. point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution;
  74. point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution;
  75. controlPoint1 = CGPointMake(0.0, 25.229);
  76. controlPoint1.x = (round(resolution * controlPoint1.x + alignStroke) - alignStroke) / resolution;
  77. controlPoint1.y = (round(resolution * controlPoint1.y + alignStroke) - alignStroke) / resolution;
  78. controlPoint2 = CGPointMake(6.771, 32.0);
  79. controlPoint2.x = (round(resolution * controlPoint2.x + alignStroke) - alignStroke) / resolution;
  80. controlPoint2.y = (round(resolution * controlPoint2.y + alignStroke) - alignStroke) / resolution;
  81. CGPathAddCurveToPoint(path, NULL, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, point.x, point.y);
  82. point = CGPointMake(295.0, 32.0);
  83. point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution;
  84. point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution;
  85. CGPathAddLineToPoint(path, NULL, point.x, point.y);
  86. CGPathCloseSubpath(path);
  87. color = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0];
  88. [color setFill];
  89. CGContextAddPath(context, path);
  90. CGContextFillPath(context);
  91. CGPathRelease(path);
  92. }
  93. 然后在你的controller中应用这个mask layer到你的UISearchBar
  94. - (void)viewDidLoad {
  95. [super viewDidLoad];
  96. SearchMaskLayer *maskLayer = [[SearchMaskLayer alloc] init];
  97. [maskLayer setFrame:CGRectMake(0, 0, 310, 34)];
  98. [maskLayer setPosition:CGPointMake(162,21)];
  99. [maskLayer setNeedsDisplay];
  100. [self.searchBar.layer setNeedsDisplay];
  101. [self.searchBar.layer setMask:maskLayer];
  102. [maskLayer release];
  103. }

2. 隐藏背景。非官方的方法。

view plaincopy to clipboardprint?
  1. for (UIView *subview in searchBar.subviews) {
  2. if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
  3. [subview removeFromSuperview];
  4. break;
  5. }
  6. }

3. 改变UISearchBar外观。 你可以子类化或category UISearchBar,然后实现两个方法,见代码。

view plaincopy to clipboardprint?
  1. - (void)drawRect:(CGRect)rect {
  2. //  UIImage *image = [UIImage imageNamed: @"background.png"];
  3. //  [image drawInRect:rect];
  4. }
  5. - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
  6. UIImage *img = [UIImage imageNamed: @"background.png"];
  7. UIImageView *v = [[[UIImageView alloc] initWithFrame:CGRectZero] autorelease];
  8. [v setImage:img];
  9. v.bounds = CGRectMake(0, 0, img.size.width, img.size.height);
  10. NSLog([NSString stringWithFormat:@"%f:%f",img.size.width, img.size.height]);
  11. NSArray *subs = self.subviews;
  12. for (int i = 0; i < [subs count]; i++) {
  13. id subv = [self.subviews objectAtIndex:i];
  14. if ([subv isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
  15. {
  16. CGRect viewRect = [subv frame];
  17. [v setFrame:viewRect];
  18. [self insertSubview:v atIndex:i];
  19. }
  20. }
  21. [v setNeedsDisplay];
  22. [v setNeedsLayout];
  23. }

UISearchBar也是一个UIView,所以你可以像对待UIView一样对待它。

转载于:https://www.cnblogs.com/zsw-1993/p/4880003.html

修改UISearchBar背景相关推荐

  1. jquery入门 修改网页背景颜色

    我们在浏览一些网站,尤其是一些小说网站的时候,都会有修改页面背景颜色的地方,这个功能使用jquery很容易实现. 效果图: show you code: <!doctype html> & ...

  2. RK3399 Ubuntu修改任务栏为自动隐藏和修改桌面背景

    挂载文件系统 源码解读: OK3399-desktop-release                                //飞凌释放的Ubuntu系统源码 OK3399-desktop- ...

  3. intellij2018修改代码背景颜色

    intellij中修改代码背景颜色 ,点击右侧background,如下图,颜色填入000000

  4. JQuery修改background-image背景图片

    JQuery修改background-image背景图片 使用: var src = ' ../resource/images/top-img.png'; $("#videoCoverLay ...

  5. Visual Studio - 修改主题背景颜色

    Visual Studio - 修改主题背景颜色 1 Dark 2 Tools -> Options 3 Environment -> General 4 Light 5 Blue Ref ...

  6. Grafana修改主题背景升级版

    Grafana 主题介绍 Grafana中有一个Panel.这个Panel是Boom Theme自带三个主题.这三个主题分别是Night Theme.Dark Theme.Light Theme. N ...

  7. 去除影像黑边-修改影像背景值-比Envi影像去除黑边-ArcGIS去除影像黑边-好用

    目录 去除黑边 一.可视化显示时去除黑边 二.输出去除黑边的影像 每一景去除黑边分别输出-操作方法 多个影像去除黑边镶嵌输出-操作方法 软件下载地址: https://pan.baidu.com/s/ ...

  8. element修改el-table 表头的背景颜色横向渐变色 + 修改表头背景颜色

    vue + element-ui 修改el-table 表头的背景颜色横向渐变色 + 修改表头背景颜色 表头背景颜色横向渐变效果图: 修改表头背景颜色和字体颜色效果图: 直接上代码: 修改表头的背景颜 ...

  9. android 仿qq聊天背景,安卓手机qq如何修改聊天背景

    如何大多数的用户都是使用安卓手机,如果我们想要修改安卓手机qq的聊天背景,应该如何修改呢?下面就让学习啦小编告诉你安卓手机qq如何修改聊天背景,希望对大家有所帮助. 安卓手机qq修改聊天背景的方法 手 ...

最新文章

  1. 超级干货:3个性能监控和优化命令详解
  2. Yii学习笔记:利用setFlash和runController打造个性化的提示信息页面
  3. python使用elasticsearch维护数据_使用Python对ElasticSearch获取数据及操作
  4. B+树(加强版多路平衡查找树)
  5. 闽高校计算机二级c语言模拟器,闽高校计算机二级C语言模拟卷及答案.doc
  6. 高频面试题2:单例设计模式
  7. mysql8导入不同schme_新特性解读 | MySQL 8.0.22 任意格式数据导入
  8. RAC环境下管理OGG-HA
  9. java json web token_一分钟简单了解JSON Web Token
  10. Visio_Premium_project_vol版
  11. Flink 异步IO
  12. 诊断Oracle数据库Hanging问题
  13. 基于CNN的IMDB电影数据集文本分类
  14. 华为展望5G未来 安全问题成关键
  15. 2020 — 只争朝夕,不负韶华
  16. 量化7年1000万知与行,回测量化7年如果只每月定投5.4万到沪深300指数基金,能否实现7年1000万
  17. 2021新上传QQ透明头像成品+源码
  18. SpringUtil 工具类
  19. 【git】------git的基本命令 (此文章转载我的老师 Alley-巷子)
  20. 【毒鸡汤】基层管理如果没有这些心态,难!

热门文章

  1. 浅析企业网站应该如何选择适合自己的服务器?
  2. 计算机交换机配置实验心得,实验六 三层交换机的配置实验报告
  3. python 遍历元组例子_【Python入门自学笔记专辑】——元组的创建、访问、遍历...
  4. 热电偶校验仪使用说明_热电偶冷端补偿方法
  5. mac brew 安装
  6. 每秒100W请求,12306秒杀业务,架构如何优化?
  7. Ruby on rails
  8. pandas demo 示例
  9. [AGC018 B] Sports Festival 解题报告
  10. Spring入门第五课