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

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

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

  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. 隐藏背景。非官方的方法。

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

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

  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一样对待它。


修改UISearchBar背景的更多相关文章

  1. 修改UISearchBar背景颜色

    UISearchBar是由两个subView组成的,一个是UISearchBarBackGround,另一个是UITextField. 要IB中没有直接操作背景的属性.方法一:是直接将 UISearc ...

  2. 修改 UISearchBar cancelButton 样式

    今天收到个问题,老大让我修改UISearchBar cancelButton的样式本来以为很简单的一个活,没想到让我长知识了. 开始在网上搜到的方法和我想象的一样,通过遍历Subviews获得butt ...

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

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

  4. 修改UISearchBar的背景颜色

    当你看到这篇博客你就已经发现了用_searchBar.backgroundColor = [UIColor clearColor];来设置UISearchBar的颜色完全没有效果: 并且,有些方法是想 ...

  5. ubuntu修改grub背景

    grub背景由/etc/grub.d/05_debian_theme定义,修改图片只需要将图片文件放到/boot/grub,d/下即可, 修改颜色只需编辑/boot/grub.d/grub.cfg

  6. Confluence 6 CSS 指南:修改顶部背景

    Confluence 默认页面的顶部是有关站点的菜单连接,在这里定义了 快速连接, 浏览菜单,用户菜单和快速查找输入框.在这个示例中,我们将会尝试修改顶部的菜单部分的背景和一些自定义的图片. 创建一个 ...

  7. android 开发 修改系统背景(状态栏颜色、导航栏颜色、标题栏颜色等等)

    1.打开values下的styles.xml 发现有以下代码: <resources> <!-- Base application theme. --> <style n ...

  8. 【BIRT】修改主题背景颜色

    下图是BIRT默认的颜色配置,为了跟系统颜色格局相一致,此处需要对颜色进行修改; 下面简单介绍了如何修改不同位置的背景颜色 对应文件地址均在目录:../webcontent/birt/styles下 ...

  9. 【VS开发】修改窗口背景颜色大全

    如何修改frame窗口的背景颜色?  MDI窗口的客户区是由frame窗口拥有的另一个窗口覆盖的.为了改变frame窗口背景的颜色,只需要这个客户区的背景颜色就可以了.你必须自己处理WM_ERASEB ...

随机推荐

  1. redis异常-MISCONF Redis is configured to save RDB snapshots

     在eclipse中用java代码通过jedis操作redis的时候,报这个错:   redis.clients.jedis.exceptions.JedisDataException: MISCON ...

  2. 集训队日常训练20180518-DIV2

    A.3232 n个物品,换取要花积分,问刚好花完积分能换最大多少价值的物品. 多重背包. #include <bits/stdc++.h> using namespace std; ]; ...

  3. CentOS 7下使用RPM安装mysql的方法。

    1.卸载系统自带的 mariadb-lib [root@centos-linux ~]# rpm -qa|grep mariadbmariadb-libs-5.5.44-2.el7.centos.x8 ...

  4. selenium(1):python3.6.4+selenium3.0+chrome环境配置

    本文为配置过程: python  1.python3.6.4下载安装见python安装说明.(本博客) 2.安装python的集成编译器PyCharm. PyCharm 是由 JetBrains 打造 ...

  5. 移动端h5禁用浏览器左滑右滑的前进后退功能

    在项目运行过程中发现,用户在有左右滑动前进后退的功能的浏览器上签字时,偶然触发了前进后退会导致canvas像是重置了一样内容消失,所以需要在代码中处理这种情况. 基本原理就是在touchmove事件中 ...

  6. excel怎么并排查看两个工作表

    excel怎么并排查看两个工作表 excel怎么并排查看两个工作表?excel打开一个窗口想要同时查看两个工作表中的数据,类似于word中的分栏效果,该怎么实现呢?EXCEL是一个使用最多的办公软件, ...

  7. DirectX11笔记(十一)--Direct3D渲染7--RENDER STATES

    原文:DirectX11笔记(十一)--Direct3D渲染7--RENDER STATES 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u010 ...

  8. 2018-5-4-WPF-获得触摸精度和触摸点

    title author date CreateTime categories WPF 获得触摸精度和触摸点 lindexi 2018-05-04 21:11:51 +0800 2018-5-4 21 ...

  9. job中shell脚本异常(删除不存在容器),导致job被打断执行的问题 脚本优化方法

    参考:  主要是添加shell的异常处理 https://www.cnblogs.com/AmilyWilly/p/7211168.html?utm_source=itdadao&utm_me ...

  10. 搭建直播服务器,使用nginx与nginx-rtmp-module搭建流媒体服务器;

    现在,一起学习一下如何自己搭建一个流媒体服务器吧! 本次搭建流媒体使用的环境是centos 7.0+nginx: 让我们一起开始奇妙的流媒体之旅吧! 1.下载nginx-rtmp-module: ng ...