如何修改系统UISearchBar的样式

        自从接触iOS快一年多的时间了,感觉自己还是菜鸟一枚,最近在整理自己的开发过程中得点点滴滴,想通过博客的形式记录下来。本博客会陆续添加内容,鉴于本人水平有限,如有错误,请给与指正,感激不尽。

专注于为中小企业提供成都做网站、成都网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业应县免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上1000+企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

        修改UISearchBar的样式,网上也有很多种,也都大同小异,本篇也不例外,但确实很实用,废话不多说,上代码:

        1.UISearchBar带有背景颜色的

         if (!_searchBar) {
        _searchBar=[[UISearchBar alloc]initWithFrame:CGRectMake(10, 20, self.view.frame.size.width-20, 40)];
        _searchBar.placeholder=@"搜索";
        _searchBar.delegate=self;
        //searchBar的背景颜色
        //1.
        [_searchBar setBackgroundColor:[UIColor orangeColor]];
        //2.关键
        [[[_searchBar.subviews objectAtIndex:0].subviews objectAtIndex:0] removeFromSuperview];
        
        
        //显示取消按钮
        _searchBar.showsCancelButton=YES;
        
        UIButton *cancelBtn=[_searchBar valueForKey:@"_cancelButton"];
        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        cancelBtn.titleLabel.font=[UIFont systemFontOfSize:14];
        [cancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        //设置光标的颜色
        _searchBar.tintColor=[UIColor redColor];
        
        
        UITextField *searchFiled=[_searchBar valueForKey:@"_searchField"];
        //设置处于编辑状态
        [searchFiled becomeFirstResponder];
        
        //输入文本的颜色
        searchFiled.textColor=[UIColor greenColor];
        
        //输入文本字体的大小
        searchFiled.font=[UIFont systemFontOfSize:14];
        
        //输入框的圆角设置
        searchFiled.layer.cornerRadius=10;
        searchFiled.layer.masksToBounds=YES;
        
        //输入框里面的背景颜色
        searchFiled.backgroundColor=[UIColor whiteColor];
        
        //提示文本的颜色
        [searchFiled setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
        
        self.cancelField=searchFiled;
        
        //输入框里的图标
        [_searchBar setImage:[UIImage p_w_picpathNamed:@"boy.jpg"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];

        [self.view addSubview:_searchBar]       

      2.UISearchBar不带背景颜色的

       //    如果不要searchBar的背景颜色
//    1.关键
    [_searchBar setBackgroundColor:[UIColor clearColor]];
    [[[_searchBar.subviews objectAtIndex:0].subviews objectAtIndex:0] removeFromSuperview];
//    2.
    UIView *searchView=[[UIView alloc]initWithFrame:CGRectMake(10, 20, self.view.frame.size.width-20, 40)];
    searchView.layer.cornerRadius=10;
    searchView.layer.borderColor=[UIColor orangeColor].CGColor;
    searchView.layer.borderWidth=1.f;
    [self.view addSubview:searchView];
    [self.view addSubview:_searchBar];

#pragram--

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    NSLog(@"实时监控文字输入");
}


- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"点击搜索按钮");
}


- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"点击取消按钮");
    [self.cancelField resignFirstResponder];
}


网页标题:如何修改系统UISearchBar的样式
本文URL:http://azwzsj.com/article/gsghoe.html