通常使用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的人有帮助。