The method of removing the background color, suitable for Ios5/6/7/8.0beta

Source: Internet
Author: User

The Uisearchbar is usually used to remove its background color to coordinate with its own interface style, but the design of Uisearchbar is constantly changing as the iOS version upgrades, Below we explore how to remove the background color by analyzing the view hierarchy of Uisearchbar in each iOS version.

First use UIView's private method recursivedescription to look at the view hierarchy of Uisearchbar in the iOS emulator versions. Here we use the Debug command PO [self.searchbar recursivedescription] to view the results as follows:
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 = ' hint phrase '; 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 = (1; 290); text = "; autoresize = w+bm; layer = <calayer:0x895 3d10>>
   | <UISearchBarBackground:0x8954230; frame = (0 0; 290); userinteractionenabled = NO; l Ayer = <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 = (290); text = "; opaque = NO; autoresize = W+BM; gesturerecog Nizers = <NSArray:0xac92fe0>; layer = <calayer:0xac92630>>
   | <UIView:0xac92860; frame = (0 0; 290); 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 = (290); text = "; opaque = NO; autoresize = W+BM; gesturerecog Nizers = <NSArray:0xa4f0e80>; layer = <calayer:0xa4ef6d0>>
   | <UIView:0xa4f06f0; frame = (0 0; 290); 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 = (290); text = "; opaque = NO; autoresize = W+BM; gesture recognizers = <NSArray:0x1405a650>; layer = <calayer:0x1405e3f0>>
   | <UIView:0x14059ec0; frame = (0 0; 290); clipstobounds = Y ES; 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>>

    as can be seen from the above, Prior to iOS7.0, the Uisearchbar view directly contained Uisearchbarbackground and Uisearchbartextfield two views, and iOS7.0 View contained a Uisearchbar view after UIView and UIView View is the Uisearchbarbackground and Uisearchbartextfield two views. After several experiments, it was found that removing the uisearchbarbackground in the Uisearchbar view Uisearchbar the background was transparent, the code is as follows:
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;
   }
}

The above code is valid for testing under Ios5/6/7/8.0beta, and I hope to be helpful to those who need to use Uisearchbar.

(turn) The method of removing the background color, suitable for Ios5/6/7/8.0beta

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.