如何修改系统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的样式
本文网址:https://www.cdcxhl.com/article4/gsghoe.html

成都网站建设公司_创新互联,为您提供品牌网站制作电子商务标签优化关键词优化网站改版企业网站制作

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

h5响应式网站建设