通常使用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的更多相关文章

  1. (转)去除背景色的方法,适合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. UISearchBar去除背景颜色

    UISearchBar *searchBar=[[UISearchBar alloc]initWithFrame:frame]; //这个设置背景透明可能无效 searchBar.background ...

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

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

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

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

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

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

随机推荐

  1. c# -- 介绍File.AppendAllText 方法

    下面介绍两个函数: File.AppendAllText (String, String) File.AppendAllText (String, String, String) File.Appen ...

  2. Rails -- 关于Migration

    Rails -- 关于Migration 无聊去逛博客,发现一篇介绍Migration,挺详细的,留个链接,以后需要的时候看. 链接: http://www.cnblogs.com/orez88/ar ...

  3. python 用gensim进行文本相似度分析

    http://blog.csdn.net/chencheng126/article/details/50070021 参考于这个博主的博文. 原理 1.文本相似度计算的需求始于搜索引擎. 搜索引擎需要 ...

  4. hihocoder1489 Legendary Items (微软2017年预科生计划在线编程笔试)

    http://hihocoder.com/problemset/problem/1489 笔试题第一道,虽然说第一道都很水,但是我感觉这题不算特别水把..这道题我就卡住了我记得,tle,最后只有30分 ...

  5. iOS 发光字流水

    { CAGradientLayer *_gradientLayer; NSInteger count; } - (void)addLabel{ // 创建UILabel UILabel *label ...

  6. 如何终止JQUERY的$.AJAX请求

    最近遇到,如果用户频繁点击ajax请求,有两个问题: 1,如果连续点击了5个ajax请求,前4个其实是无效的,趁早结束节省资源. 2,更严重的问题是:最后一个发送的请求,响应未必是最后一个,有可能造成 ...

  7. 吐血推荐250部必看电影下载 IMDB TOP 250 download

    中文名: IMDB Top 250合辑 TLF-MiniSD收藏版英文名: IMDB Top 250 TLF-MiniSD Collection版本: (更新至TOP119)[MiniSD]发行日期: ...

  8. delphi tcp/ip IdTCPServer1实例一

    unit Unit1; interface uses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Form ...

  9. Assembly.Load()方法,Assembly.LoadFrom()方法,Assembly.LoadFile()方法的区别!

    参考: http://www.cnblogs.com/benwu/archive/2009/10/24/1589096.html http://www.cnblogs.com/xuefeng1982/ ...

  10. Assignment (HDU 2853 最大权匹配KM)

    Assignment Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...