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的人有帮助。

时间: 2024-10-10 10:42:42

UISearchbar去除背景色的方法,适合iOS5/6/7/8.0beta的相关文章

(转)去除背景色的方法,适合iOS5/6/7/8.0beta

通常使用UISearchbar都需要去除其背景色来与自己的界面风格保持协调,但是UISearchbar的设计随着iOS版本的升级不断地在发生着变化,下面我们通过分析UISearchbar在各个iOS版本下的视图层次结构来探讨去除其背景色的方法. 首先使用UIView的私有方法recursiveDescription来看一下UISearchbar在iOS各个模拟器版本中的视图层次结构.这里使用调试命令po [self.searchBar recursiveDescription]来查看,结果如下:

iOS 开发之 - iOS6设置UISearchbar的背景色

效果图: ios 6 默认: 修改之后: 代码: if (!IsIOS7) { [[ _searchBar.subviews objectAtIndex : 0 ] removeFromSuperview ]; [ _searchBar setBackgroundColor :[ UIColor clearColor ]]; } mark 为我成为大神做铺垫. iOS 开发之 - iOS6设置UISearchbar的背景色

自定义Button背景色的方法

//自定义button设置背景色的方法 - (void)setBackgroundColor:(UIColor *)color forState:(UIControlState)state{ [self setBackgroundImage:[self buttonImageFromColor:color] forState:state]; } - (UIImage *) buttonImageFromColor:(UIColor *)color { CGRect rect = CGRectMa

更改UISearchBar系统背景色方法

1. searchBar.searchBarStyle = UISearchBarStyleMinimal; 2. searchBar.backgroundImage = [self imageWithColor:[UIColor clearColor]]; - (UIImage *)imageWithColor:(UIColor *)color { CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); UIGraphicsBeginImageCon

UISearchBar去除背景颜色

UISearchBar *searchBar=[[UISearchBar alloc]initWithFrame:frame]; //这个设置背景透明可能无效 searchBar.backgroundColor=[UIColor clearColor]; //可采用如下方法去除搜索框背景 for (UIView *view in searchBar.subviews) { // iOS7.0之前 if ([view isKindOfClass:NSClassFromString(@"UISear

xcode 5.0 以上去掉icon高亮方法&amp;amp;iOS5白图标问题

之前的建议方法是把在xxx.info.plist文件里把 icon already includes gloss and bevel effects 设置YES 在Xcode5下,重复实现不成功,今天最终找到解决方式,假设使用xcassets设置方法,须要选择iOS icon is pre-rendered 的选择框,如图 xcod5在iOS5下白图标问题, http://iphonedevsdk.com/forum/iphone-sdk-development/115604-ipad-icon

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

在webView的delegate回调方法    - (void)webViewDidFinishLoad:(UIWebView *)webView;中写上一下语句即可 //字体大小 [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '330%'"]; //字体颜色 [webView stringBy

display:inline-block; 去除间隙的方法 总结:

个人常用: 如: <ul> <li><a href="#" >实时数据</a></li> <li><a href="#">地图展示</a></li> <li><a href="#">乡镇街道</a></li> <li><a href="#">近期气象

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

方法一: 优点:无需替换文件,无需任何工具,对系统没有副作用缺点:更换主题或者壁纸之后水印再现方法:按住 “win键+P” 进入 “第二屏幕 ”选择 “扩展”再按住 “win键+P” 进入 “第二屏幕 ”选择 “仅电脑屏幕”关机重启水印也不会出现. 方法二: 开机自动去除Win8未激活水印:1.新建立个快捷方式 – 输入 cmd.exe /c taskkill.exe /f /im explorer.exe && start explorer.exe2.把快捷方式丢到开机自启动目录3.开机