更改IOS于UISearchBar撤消button底、搜索输入文本框背景中的内容和UISearchBar底

本文详细介绍如何自定义iOS应用中的UISearchBar样式,包括调整背景、图标、输入框样式及取消按钮的文字颜色与背景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 转载请标明出处:http://blog.youkuaiyun.com/android_ls/article/details/39993433

測试的手机IOS系统版本为:6.1.3,实现过程例如以下:

1、加入UISearchBar到父View

    _searchBar = [[UISearchBar alloc]init];
    _searchBar.frame = CGRectMake(0, 0, self.view.frame.size.width, kSeachBarH);
    _searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    _searchBar.delegate = self;
    _searchBar.placeholder = @"请输入姓名、公司名称、公司产品名称";
    [self.view addSubview:_searchBar];
2、改动搜索框背景
    UIImage *img = [UIImage resizedImage:@"find_bg.png"];
    [_searchBar setBackgroundImage:img];
3、改动搜索输入框内左側的指示图标

    [_searchBar setImage:[UIImage resizedImage:@"ic_search.png"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];

4、改动搜索输入文本的背景

    [_searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"login_btn_input_side.png"] forState:UIControlStateNormal];

     注:对于设计人员提供的搜索输入文本的背景。若提供的是一个圆角的小方块,按常理我们会使用拉伸图片的中间部分的方法,经測试显示效果例如以下:


     若让设计人员又一次提供一张固定高度的图片(比方高是60),当做搜索输入文本的背景。效果图例如以下:


5、改动UISearchBar右側的取消button文字颜色及背景图片

#pragma mark 搜索框的代理方法,搜索输入框获得焦点(聚焦)
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    [searchBar setShowsCancelButton:YES animated:YES];

    // 改动UISearchBar右側的取消button文字颜色及背景图片
    for (UIView *searchbuttons in [searchBar subviews]){
        if ([searchbuttons isKindOfClass:[UIButton class]]) {
            UIButton *cancelButton = (UIButton*)searchbuttons;
            // 改动文字颜色
            [cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
            
            // 改动button背景
            [cancelButton setBackgroundImage:[UIImage resizedImage:@"login_btn_login.png"] forState:UIControlStateNormal];
            [cancelButton setBackgroundImage:nil forState:UIControlStateHighlighted];
        }
    }
}
注:改动取消button文字颜色及背景图片的代码片段,一定要放到取消button会显示代理方法中改动,否则遍历找不着呀,那就改动不了了。



版权声明:本文博主原创文章,博客,未经同意不得转载。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值