博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【IOS】UISearchBar背景透明,去掉背景,自定义背景
阅读量:6486 次
发布时间:2019-06-24

本文共 3041 字,大约阅读时间需要 10 分钟。

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 }

 

转载于:https://www.cnblogs.com/DannyApple/p/3969393.html

你可能感兴趣的文章
Python实现设置终端显示颜色、粗体、下划线等效果
查看>>
Druid-基本概念
查看>>
[LeetCode] Majority Element
查看>>
Html5实现手机九宫格密码解锁功能
查看>>
scala处理日期
查看>>
ENode 2.0 - 深入分析ENode的内部实现流程和关键地方的幂等设计
查看>>
【日常小记】linux中强大且常用命令:find、grep
查看>>
聊聊jvm的-XX:MaxDirectMemorySize
查看>>
(十二)java springcloud b2b2c多用户商城系统-springboot集成apidoc
查看>>
吴恩达机器学习 Coursera 笔记(三) - 线性回归回顾
查看>>
新鲜出炉的电信诈骗经历
查看>>
SpringCloud源码:Ribbon负载均衡分析
查看>>
vue中axios请求的封装
查看>>
深入Java -JVM 垃圾回收
查看>>
工作多年的.NET程序员,是否建立了自己的开发知识库?分享制作电子书的经验...
查看>>
网络端口号大全
查看>>
非常不错的sharepoint webpart工具集
查看>>
腾讯微博Android客户端开发——OAuth认证介绍
查看>>
selinux 设置关键命令行
查看>>
TPFanControl v0.62 + 汉化补丁
查看>>