【IOS】UISearchBar背景透明,去掉背景,自訂背景

來源:互聯網
上載者:User

標籤:style   blog   color   io   os   ar   for   art   div   

ios6,ios7,ios7.1下設定UISearchbar的背景色

ios系統升級到7.1後,原來在7.0下顯示正常的UISearchbar現在又出現問題了。究其原因,是由於UISearchbar的subview又做修改了。

 1 //修改searchBar樣式 2  3 - (void)changeSearchBar { 4  5     float version = [[[UIDevice currentDevice] systemVersion] floatValue]; 6  7     if ([_searchBar respondsToSelector:@selector(barTintColor)]) { 8  9         float iosversion7_1 = 7.1;10 11         if (version >= iosversion7_1)        {12 13             //iOS7.114 15             [[[[_searchBar.subviews objectAtIndex:0] subviews] objectAtIndex:0] removeFromSuperview];16 17             [_searchBar setBackgroundColor:[UIColor clearColor]];18 19         }20 21         else {            //iOS7.022 23             [_searchBar setBarTintColor:[UIColor clearColor]];24 25             [_searchBar setBackgroundColor:[UIColor clearColor]];26 27         }28 29     }30 31     else {32 33         //iOS7.0以下34 35         [[_searchBar.subviews objectAtIndex:0] removeFromSuperview];36 37 //        [_searchBar setBackgroundColor:[UIColor clearColor]];38 39         [_searchBar setBackgroundColor:[UIColor colorWithPatternImage:[UIImageimageNamed:@"searchBarBg.png"]]];40 41     }}

 

 

//一行代碼搞定去掉去掉背景

[[searchbar.subviews objectAtIndex:0]removeFromSuperview];

----------------------------------------------------------------------

 

注意:

去掉搜尋控制項的背景之後在下方會出現一條黑線,我們要把那條黑線去除

1 CGRect rect = self.searchBar.frame;2 UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, rect.size.height-2,rect.size.width, 2)];3 lineView.backgroundColor = [UIColor whiteColor];4 [self.searchBar addSubview:lineView];

 

 1 /*UISearchBar背景透明,去掉背景,自訂背景*/ 2  3 seachBar=[[UISearchBar alloc] init]; 4  5   6  7 //修改搜尋方塊背景 8  9 seachBar.backgroundColor=[UIColor clearColor];10 11 //去掉搜尋方塊背景12 13 //1.14 15 [[searchbar.subviews objectAtIndex:0]removeFromSuperview];16 17 //2.18 19 for (UIView *subview in seachBar.subviews) 20 21 {  22 23 if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])24 25 {  26 27 [subview removeFromSuperview];  28 29 break;  30 31 }  32 33 } 34 35 //3自訂背景36 37 UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"40-di.png"]];38 39     [mySearchBar insertSubview:imageView atIndex:1];40 41     [imageView release];42 43 //4輸入搜尋文字時隱藏搜尋按鈕,清空時顯示44 45 - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {  46 47 searchBar.showsScopeBar = YES;  48 49 [searchBar sizeToFit];  50 51 [searchBar setShowsCancelButton:YES animated:YES];  52 53 return YES;  54 55 }  56 57 - (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {  58 59 searchBar.showsScopeBar = NO;  60 61 [searchBar sizeToFit];  62 63 [searchBar setShowsCancelButton:NO animated:YES];  64 65 return YES;  66 67 }  68 69 //改變搜尋按鈕文字70 71 //改變UISearchBar取消按鈕字型72 73 for(id cc in [searchBar subviews])74 75     {76 77 if([cc isKindOfClass:[UIButton class]])78 79         {80 81             UIButton *btn = (UIButton *)cc;82 83             [btn setTitle:@"搜尋"  forState:UIControlStateNormal];84 85         }86 87 }

 

【IOS】UISearchBar背景透明,去掉背景,自訂背景

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.