UISearchbar去除背景色的方法,适合iOS5/6/7/8.0beta
通常使用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的人有帮助。
UISearchbar去除背景色的方法,适合iOS5/6/7/8.0beta的更多相关文章
- (转)去除背景色的方法,适合iOS5/6/7/8.0beta
通常使用UISearchbar都需要去除其背景色来与自己的界面风格保持协调,但是UISearchbar的设计随着iOS版本的升级不断地在发生着变化,下面我们通过分析UISearchbar在各个iOS版 ...
- oracle中如何对字符串进行去除空格的方法
oracle中如何对字符串进行去除空格的方法 今天学习了一下oracle中如何对字符串进行去除空格的方法,这里总结一下.了解到的方法主要有两种:Trim函数以及Replace函数.下面我详细的介绍一下 ...
- Python中常见字符串去除空格的方法总结
Python中常见字符串去除空格的方法总结 1:strip()方法,去除字符串开头或者结尾的空格>>> a = " a b c ">>> a.s ...
- WPF去除边框的方法
原文:WPF去除边框的方法 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/yangsen600/article/details/81978125 W ...
- python 中去除空格的方法
python 中去除空格的方法: def trim(s): l=[] for i in s: if i!=' ': l.append(i) return ''.join(l) 其中可以使用下面的 '' ...
- UISearchBar去除背景颜色
UISearchBar *searchBar=[[UISearchBar alloc]initWithFrame:frame]; //这个设置背景透明可能无效 searchBar.background ...
- iOS 更改webView文字颜色丶文字大小丶背景色的方法
在webView的delegate回调方法 - (void)webViewDidFinishLoad:(UIWebView *)webView;中写上一下语句即可 //字体大小 [webView ...
- java中,去除空白的方法
有时候,我们页面传过来的值,或者做excel导入时填入的值都需要去掉像空格一样的一些特殊字符,下面这个方法可去掉像制表符,换行键,回车,空格或者不在ACSII中 的特殊字符 /** * 去除字符串开始 ...
- 【repost】如何学好编程 (精挑细选编程教程,帮助现在在校学生学好编程,让你门找到编程的方向)四个方法总有一个学好编程的方法适合你
方法(一)编了这么久的程序,一直想找机会总结下其中的心得和方法,但回想我这段编程道路,又很难说清楚,如果按照我走过的所有路来说,显然是不可能的!当我看完了云风的<游戏之旅--编程感悟>和梁 ...
随机推荐
- 2349 Arctic Network(中文版)
试题描述: 国防部希望通过无线网络连接几个北方前哨基地. 在建立网络时将使用两种不同的通信技术:每个前哨基站都将拥有无线电收发器,另外还有一些前哨卫星通道. 任何带卫星频道的两个前哨都可以通过卫星进行 ...
- Ubuntu 16.04安装uafred用于替代Alfred
说明: Macx下的Alfred非常强大,在Ubuntu下的替代为uafred,为什么选择uafred而不选择其它在于其功能开发简单,模块集成方便,不像别的实现是那种来回封装调用,其实就是一个功能而已 ...
- Added components improve switching-regulator stability
Added components improve switching-regulator stability
- [制作实践]一种基于LM2576的多功能开关电源设计
http://bbs.kechuang.org/read-kc-tid-9837-page-e.html 摘要:本文介绍了一种性价比高.功能丰富的程控开关电源的设计,对基于LM2576控制核心的升.降 ...
- 如何屏蔽PHP浏览器头信息X-Powered-By
将 php.ini 中,将 “expose_php = On” 改为 “expose_php = Off”
- 【多线程】java多线程Completablefuture 详解【在spring cloud微服务之间调用,防止接口超时的应用】【未完成】
参考地址:https://www.jianshu.com/p/6f3ee90ab7d3 示例: public static void main(String[] args) throws Interr ...
- 关于LightMapping和NavMesh烘焙的动态载入
熟悉unity的朋友都应该知道,unity有内部LightMapping烘焙和NavMesh寻路的功能.但这些非常好用的功能,都是基于对某个已经保存的关卡(scene)进行烘焙(Bake)的操作,我一 ...
- OpenShift跨版本升级
官方的in-place upgrade直接在线升级的参考链接 https://docs.openshift.com/container-platform/3.11/upgrading/automate ...
- OpenCV学习(11) 图像的腐蚀与膨胀(2)
先对一副灰度图像进行腐蚀操作,然后在腐蚀后的图像上再进行膨胀操作,我们定义这个操作为开操作. 先对一副图像进行膨胀操作,然后在膨胀后的图像上再进行腐蚀操作,我们定义这个操作为闭操作. 开操 ...
- php 单进程SAPI生命周期
php的生命周期3.1 STARTUP 1.初始化引擎和核心组件. 2.解析php.ini. 3.初始化静态构建的模块(MINIT). 4.初始化共享模块(MINIT).3.2 ...