通常使用UISearchbar都需要去除其背景色来与自己的界面风格保持协调,但是UISearchbar的设计随着iOS版本的升级不断地在发生着变化,下面我们通过分析UISearchbar在各个iOS版本下的视图层次结构来探讨去除其背景色的方法。

首先使用UIView的私有方法recursiveDescription来看一下UISearchbar在iOS各个模拟器版本中的视图层次结构。这里使用调试命令po [self.searchBar recursiveDescription]来查看,结果如下:
ios5.0:
<UISearchBar: 0x76c96c0; frame = (27 1; 290 44); text = ''; autoresize = W+BM; layer = <CALayer: 0x76c9870>>
   | <UISearchBarBackground: 0x76b3d40; frame = (0 0; 290 44); userInteractionEnabled = NO; layer = <CALayer: 0x76ad4a0>>
   | <UISearchBarTextField: 0x76c9e40; frame = (5 6; 280 31); text = ''; clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x76c9fa0>>
   |    | <UITextFieldBorderView: 0x76e9e70; frame = (0 0; 280 31); opaque = NO; layer = <CALayer: 0x76e9f00>>
   |    | <UIImageView: 0x76cce70; frame = (10 8; 15 15); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x76cceb0>>
   |    | <UITextFieldLabel: 0x76cf630; frame = (32 7; 216 18); text = '提示语'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x76cb2c0>>

ios5.1:
<UISearchBar: 0x8378110; frame = (27 1; 290 44); text = ''; autoresize = W+BM; layer = <CALayer: 0x83782e0>>
   | <UISearchBarBackground: 0x8378630; frame = (0 0; 290 44); userInteractionEnabled = NO; layer = <CALayer: 0x83786c0>>
   | <UISearchBarTextField: 0x8378e50; frame = (0 0; 0 0); text = ''; clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x8378f90>>

ios6.0:
<UISearchBar: 0xa13fc60; frame = (27 1; 290 44); text = ''; autoresize = W+BM; layer = <CALayer: 0xa13fe40>>
   | <UISearchBarBackground: 0xa140380; frame = (0 0; 290 44); userInteractionEnabled = NO; layer = <CALayer: 0xa140440>>
   | <UISearchBarTextField: 0xa140b90; frame = (0 0; 0 0); text = ''; clipsToBounds = YES; opaque = NO; gestureRecognizers = <NSArray: 0xa1428b0>; layer = <CALayer: 0xa140cf0>>

ios6.1:
<UISearchBar: 0x8953b20; frame = (27 1; 290 44); text = ''; autoresize = W+BM; layer = <CALayer: 0x8953d10>>
   | <UISearchBarBackground: 0x8954230; frame = (0 0; 290 44); userInteractionEnabled = NO; layer = <CALayer: 0x89542f0>>
   | <UISearchBarTextField: 0x8954a30; frame = (0 0; 0 0); text = ''; clipsToBounds = YES; opaque = NO; gestureRecognizers = <NSArray: 0x8956630>; layer = <CALayer: 0x8954b90>>

ios7.0:
<UISearchBar: 0xac923d0; frame = (27 21; 290 44); text = ''; opaque = NO; autoresize = W+BM; gestureRecognizers = <NSArray: 0xac92fe0>; layer = <CALayer: 0xac92630>>
   | <UIView: 0xac92860; frame = (0 0; 290 44); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0xac928c0>>
   |    | <UISearchBarBackground: 0xac93320; frame = (0 0; 290 44); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xac93440>>
   |    | <UISearchBarTextField: 0xac93940; frame = (0 0; 0 0); text = ''; clipsToBounds = YES; opaque = NO; gestureRecognizers = <NSArray: 0xac953a0>; layer = <CALayer: 0xac93b20>>
   |    |    | <_UISearchBarSearchFieldBackgroundView: 0xac98520; frame = (0 0; 0 0); opaque = NO; autoresize = W+H; userInteractionEnabled = NO; layer = <CALayer: 0xac985d0>>

ios7.1:
<UISearchBar: 0xa4ef3e0; frame = (27 21; 290 44); text = ''; opaque = NO; autoresize = W+BM; gestureRecognizers = <NSArray: 0xa4f0e80>; layer = <CALayer: 0xa4ef6d0>>
   | <UIView: 0xa4f06f0; frame = (0 0; 290 44); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0xa4f0750>>
   |    | <UISearchBarBackground: 0xa4f11b0; frame = (0 0; 290 44); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xa4f1300>>
   |    | <UISearchBarTextField: 0xa4f1870; frame = (0 0; 0 0); text = ''; clipsToBounds = YES; opaque = NO; layer = <CALayer: 0xa4f1a80>>
   |    |    | <_UISearchBarSearchFieldBackgroundView: 0xa4f4880; frame = (0 0; 0 0); opaque = NO; autoresize = W+H; userInteractionEnabled = NO; layer = <CALayer: 0xa4f49e0>>

ios8.0beta:
<UISearchBar: 0x1405e140; frame = (27 21; 290 44); text = ''; opaque = NO; autoresize = W+BM; gestureRecognizers = <NSArray: 0x1405a650>; layer = <CALayer: 0x1405e3f0>>
   | <UIView: 0x14059ec0; frame = (0 0; 290 44); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x14059f30>>
   |    | <UISearchBarBackground: 0x1405a9d0; frame = (0 0; 290 44); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x1405ab20>>
   |    | <UISearchBarTextField: 0x1405b0b0; frame = (0 0; 0 0); text = ''; clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x1405b2d0>>
   |    |    | <_UISearchBarSearchFieldBackgroundView: 0x140609b0; frame = (0 0; 0 0); opaque = NO; autoresize = W+H; userInteractionEnabled = NO; layer = <CALayer: 0x14060b10>>

从以上可以看出,在iOS7.0之前,UISearchbar视图里直接包含UISearchBarBackground和UISearchBarTextField两个视图,在iOS7.0及之后,UISearchbar视图里包含的是一个UIView视图,UIView视图里才是UISearchBarBackground和UISearchBarTextField两个视图。经多次试验,发现去除UISearchbar视图里的UISearchBarBackground后UISearchbar的背景就透明了,代码如下:
for (UIView *view in self.searchBar.subviews) {
    // for before iOS7.0
    if ([view isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
        [view removeFromSuperview];
        break;
    }
    // for later iOS7.0(include)
    if ([view isKindOfClass:NSClassFromString(@"UIView")] && view.subviews.count > 0) {
        [[view.subviews objectAtIndex:0] removeFromSuperview];
        break;
    }
}

以上代码在iOS5/6/7/8.0beta下测试均有效,希望对需要使用UISearchBar的人有帮助。

 

(转)去除背景色的方法,适合iOS5/6/7/8.0beta的更多相关文章

  1. UISearchbar去除背景色的方法,适合iOS5/6/7/8.0beta

    通常使用UISearchbar都须要去除其背景色来与自己的界面风格保持协调,可是UISearchbar的设计随着iOS版本号的升级不断地在发生着变化.以下我们通过分析UISearchbar在各个iOS ...

  2. oracle中如何对字符串进行去除空格的方法

    oracle中如何对字符串进行去除空格的方法 今天学习了一下oracle中如何对字符串进行去除空格的方法,这里总结一下.了解到的方法主要有两种:Trim函数以及Replace函数.下面我详细的介绍一下 ...

  3. Python中常见字符串去除空格的方法总结

    Python中常见字符串去除空格的方法总结 1:strip()方法,去除字符串开头或者结尾的空格>>> a = " a b c ">>> a.s ...

  4. WPF去除边框的方法

    原文:WPF去除边框的方法 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/yangsen600/article/details/81978125 W ...

  5. python 中去除空格的方法

    python 中去除空格的方法: def trim(s): l=[] for i in s: if i!=' ': l.append(i) return ''.join(l) 其中可以使用下面的 '' ...

  6. iOS 更改webView文字颜色丶文字大小丶背景色的方法

    在webView的delegate回调方法    - (void)webViewDidFinishLoad:(UIWebView *)webView;中写上一下语句即可 //字体大小 [webView ...

  7. java中,去除空白的方法

    有时候,我们页面传过来的值,或者做excel导入时填入的值都需要去掉像空格一样的一些特殊字符,下面这个方法可去掉像制表符,换行键,回车,空格或者不在ACSII中 的特殊字符 /** * 去除字符串开始 ...

  8. 【repost】如何学好编程 (精挑细选编程教程,帮助现在在校学生学好编程,让你门找到编程的方向)四个方法总有一个学好编程的方法适合你

    方法(一)编了这么久的程序,一直想找机会总结下其中的心得和方法,但回想我这段编程道路,又很难说清楚,如果按照我走过的所有路来说,显然是不可能的!当我看完了云风的<游戏之旅--编程感悟>和梁 ...

  9. Windows8 正式版最简单的去除桌面水印方法

    方法一: 优点:无需替换文件,无需任何工具,对系统没有副作用缺点:更换主题或者壁纸之后水印再现方法:按住 “win键+P” 进入 “第二屏幕 ”选择 “扩展”再按住 “win键+P” 进入 “第二屏幕 ...

随机推荐

  1. 洛谷P2599||bzoj1413 [ZJOI2009]取石子游戏

    bzoj1413 洛谷P2599 根本不会啊... 看题解吧 #include<cstdio> #include<algorithm> #include<cstring& ...

  2. linux 设置固定IP centOS6.5

    主要是要把Linux的IP固定下来,可以用另一台机器SSH连接. ping的用法: 基本语法:ping [-选项] IP地址或域名 功能描述:测试网络是否连通 常用选项:-c -c 指定发送数据包的次 ...

  3. 创建Maven项目出错 pom出错

    错误为 org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.maven.project.MavenProject, org.a ...

  4. CSS修改Autocomplete样式

    举个场景:在用户登录成功的时候,浏览器有时候会咨询你是否记住密码,当你记住密码下次登录的时候,标签会自动填充,但是这样会造成样式不统一,解决办法如下: input:-webkit-autofill, ...

  5. IOS之constraints

    anyway, you can do this with auto layout. You can do it entirely in IB as of Xcode 5.1. Let's start ...

  6. Cairo Drawing Model

    Cairo Drawing Model Cairo是一个强力的2D绘图库. Destination 是你最终绘图的目标, 可以是一系列Pixel或者绑定到SVG或PDF文件上. Source 是实际在 ...

  7. SpringMVC、Spring和Struts的区别

    http://www.cnblogs.com/hhx626/p/6010293.html 导读:近期做到的项目中,用到的框架师SSM(SpringMVC+Spring+Mybatis),那么在这之前用 ...

  8. MIPS汇编程序设计——四则运算计算器

    实验目的 运用简单的MIPS实现一个能够整数加减乘除的计算器,同时使自己更加熟悉这些指令吧 MIPS代码 #sample example 'a small calculater’ # data sec ...

  9. 单源最短路Dijstra

    #include<iostream> #include<cstring> #define INF 0x3f3f3f3f using namespace std; ][],d[] ...

  10. bfs染色法判定二分图

    #include<iostream> #include<queue> #include<cstring> #include<cstdio> using ...