iOS自定义searchBar

定制UISearchBar样式
本文介绍两种自定义UISearchBar背景的方法。方法一通过移除默认背景或添加自定义背景实现;方法二则是通过替换背景视图来达到同样的效果。此外还介绍了如何修改搜索按钮的文字。

//    UISearchBar是由两个subView组成的,一个是UISearchBarBackGround,另一个是UITextField. IB中没有直接操作背景的属性。方法一:是直接将 UISearchBarBackGround移去。方法二:是创建一个UIView设置其颜色加载到UISearchBarBaceGround上作为UISearchBar的背景颜色


//        方法一:

        UISearchBar *seachBar=[[UISearchBar alloc] initWithFrame:CGRectMake(0, 150, 320, 40)];

        //修改搜索框背景

        seachBar.backgroundColor=[UIColor whiteColor];

        seachBar.placeholder = @"search";

        //去掉搜索框背景

        //1.

//        [[seachBar.subviews objectAtIndex:0] removeFromSuperview];

        //2.

//        for (UIView *subview in seachBar.subviews)

//        {

//            if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])

//            {

//                [subview removeFromSuperview];

//                break;

//            }

//        }

        //3自定义背景

        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"12.png"]];

        [seachBar insertSubview:imageView atIndex:1];

    //改变搜索按钮文字

    //改变UISearchBar取消按钮字体

//    for(id cc in [seachBar subviews])

//    {

//        if([cc isKindOfClass:[UIButton class]])

//        {

//            UIButton *btn = (UIButton *)cc;

//            [btn setTitle:@"搜索" forState:UIControlStateNormal];

//            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

//        }

//    }

        [self.view addSubview:seachBar];

    

    

//    方法二:

    UISearchBar* my_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 150, 320, 41)];

    my_searchBar.delegate = self;

//    m_searchBar.barStyle =UIBarStyleBlackTranslucent;

    my_searchBar.autocorrectionType = UITextAutocorrectionTypeNo;

    my_searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;

    my_searchBar.placeholder = @"Search";

    my_searchBar.keyboardType = UIKeyboardTypeDefault;

    //--->背景图片

    UIView *segment = [my_searchBar.subviews objectAtIndex:0];

    UIImageView *bgImage = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"12.png"]];

    [segment addSubview: bgImage];

    

    [self.view addSubview:my_searchBar];



转载请注明出处:http://blog.youkuaiyun.com/sevenquan



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值