iOS自定义searchBar

//    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.csdn.net/sevenquan



你可能感兴趣的:(iOS给搜索条添加背景图片,iOS自定义搜索条,iOS自定义searchBar)