IOS學習 IOS8的UISearchBar對應
通常使用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的人有幫助。
--------------------------------------
以上是轉載內容,下面寫個自己的實現方案:
在xib中可以設定的backgroundImage是載入到UISearchBarBackground層上的image,在ios7.0以及之後的系統上只要設定一個透明的圖片,就可以在不刪除UISearchBarBackground的情況下達到透過UISearchBarBackground的目的。設定UISearchBar的BackgroundColor即可。
ios7.0之前的版本可以通過設定UISearchBar的tintcolor實現設定顏色。
備註:UISearchBarTextField的frame只是textfield那麼大小
相關文章
- iOS UISearchBar 修改背景顏色iOSUI
- IOS UISearchBar UISearchController使用iOSUIController
- iOS swift UISearchBar拿到textfield控制元件iOSSwiftUI控制元件
- iOS8本地推送iOS
- iOS定製UISearchBar導航欄 同步iOS11iOSUI
- IOS8模糊毛玻璃的效果UIVisualEffectViewiOSUIView
- iOS-OC-UISearchBar取消搜尋狀態iOSUI
- ios8系統定位問題iOS
- iOS8新特性-UIAlertControlleriOSUIController
- iOS8 之 PrefixHeader.pchiOSHeader
- iOS:iOS8開發 深入理解autolayout(3)iOS
- iOS8相機授權判斷iOS
- 終極指南:如何為iOS8應用製作預覽視訊iOS
- 學習iOS開發前應該知道的(1)iOS
- 學習iOS開發前應該知道的(2)iOS
- iOS8 自定義鈴聲 mp3iOS
- iOS8中UIAlertView和UIActionSheet河裡去了?iOSUIView
- WebGL 3D on iOS8 正式版Web3DiOS
- Ios8之後, 定位的delegate不能觸發的問題iOS
- iOS:iOS8開發storyboard中autolayout和size class的使用詳解 (2)iOS
- iOS cell找對應的tableViewiOSView
- iOS學習iOS
- 學習iOSiOS
- UISearchBar預設高度UI
- UISearchBar常用屬性UI
- iOS8 Framework使用載入xib及圖片iOSFramework
- WebGL on iOS8 終於等到了這一天WebiOS
- iOS SDWebImage 學習iOSWeb
- IOS學習(0)iOS
- IOS動畫學習iOS動畫
- IOS 單例模式的學習iOS單例模式
- dissmiss a UISearchBar with an SearchBarControllerSSMUIController
- iOS-Blocks學習iOSBloC
- IOS學習計劃iOS
- Vue學習筆記(二)------axios學習Vue筆記iOS
- 對深度學習的認識深度學習
- iOS8 自定義UITabBar (使用popToViewController導致的UITabBarButton重疊的問題)iOSUItabBarViewController
- abap學習筆記-SAP欄位與表的對應關係筆記