修改searchbar的背景颜色

来源:互联网 发布:linux chromium 安装 编辑:程序博客网 时间:2024/05/16 15:11

UISearchBar是由两个subView组成的,一个是UISearchBarBackGround,另一个是UITextField. 

方法一:是直接将 UISearchBarBackGround移去。

方法二:是创建一个UIView设置其颜色加载到UISearchBarBaceGround上作为UISearchBar的背景颜色


核心代码如下:

-(void)removeBgGroundView{    //1.//    [[self.subviews objectAtIndex:0]removeFromSuperview];    //2.    for (UIView *subview in self.subviews)    {        NSLog(@"%@,",self.subviews);        if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])        {            [subview removeFromSuperview];            break;        }    }    //3自定义背景    UIImageView *imageView = [[UIImageView alloc] init];    imageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);    imageView.backgroundColor = [UIColor whiteColor];    [self insertSubview:imageView atIndex:1];}


原创粉丝点击