去除背景色的方法,適合iOS5/6/7/8.0beta

來源:互聯網
上載者:User

標籤:

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

 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.