iOS自定义searchBar


@property (nonatomic,retain)UIView *myheaderView;

@property (nonatomic,retain)UILabel *addressL;

@property(nonatomic,retain)UISearchBar *mySearchBar;


我是隐藏了导航栏,在上面加一个View(self.myheaderView


1.创建self.myheaderView 

    self.myheaderView = [[UIViewalloc]init];

    self.myheaderView.backgroundColor =HEADVC;

    self.myheaderView.userInteractionEnabled =YES;

    [self.viewaddSubview:self.myheaderView];

    [self.myheaderViewmas_makeConstraints:^(MASConstraintMaker *make) {

        make.right.left.top.mas_equalTo(@0);

        make.size.mas_equalTo(CGSizeMake(self.view.frame.size.width,139.00*AUTO_HEIGHT/2));

        make.size.mas_equalTo(CGSizeMake(self.view.frame.size.width,139.00*AUTO_WIDTH/2));

    }];

2.在self.myheaderView 上添加一个View(searchView)


searchView = [ UIViewnew];

    searchView.backgroundColor = [UIColorcolorWithRed:0.133green:0.149blue:0.173alpha:0.700];

    [self.myheaderViewaddSubview:searchView];

    [searchView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.mas_equalTo(25 *AUTO_HEIGHT);

        make.left.mas_equalTo(15*AUTO_WIDTH);

        make.height.mas_equalTo(139 /2 * AUTO_HEIGHT - 35 *AUTO_HEIGHT);

        make.width.mas_equalTo (SCREEN_WIDTH -70 * AUTO_WIDTH);

        

    }];

    //裁掉边框

    searchView.layer.masksToBounds =YES;

    //设置半径

    searchView.layer.cornerRadius =18 *AUTO_HEIGHT;

3.添加中国Label

    _addressL  = [UILabelnew];

    [searchView addSubview:_addressL];

    [_addressL mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.mas_equalTo(5 *AUTO_HEIGHT);

        make.left.mas_equalTo(10 *AUTO_WIDTH);

        make.bottom.mas_equalTo(searchView.mas_bottom).offset( -4 * AUTO_HEIGHT);

        

        

    }];

    _addressL.text =@"中国";

    _addressL.textAlignment =NSTextAlignmentCenter;

    _addressL.font = [UIFontfontWithName:@"Arial"size:TEXTCONTENTFONT2 *AUTO_WIDTH];

    _addressL.textColor = [UIColorwhiteColor];

    _addressL.userInteractionEnabled =YES;

4.添加三角图片

    UIImageView *addImageV = [UIImageViewnew];

    [searchView addSubview:addImageV];

    [addImageV mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.mas_equalTo(_addressL.mas_top).offset(10 *AUTO_HEIGHT);

        make.left.mas_equalTo(_addressL.mas_right).offset(4 *AUTO_WIDTH);

        make.size.mas_equalTo(CGSizeMake(11 *AUTO_WIDTH, 7 *AUTO_HEIGHT));

        

        

    }];

    addImageV.image = [UIImageimageNamed:@"search三角"];

5.给中国_addressL 添加手势 (在这里并没有什么作用)

  UITapGestureRecognizer *tap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(addressTap:)];

    [_addressLaddGestureRecognizer:tap];

6.加线

  UIImageView *line = [UIImageViewnew];

    line.backgroundColor =LINE;

    line.alpha = 0.6;

    [searchView addSubview:line];

    [line mas_makeConstraints:^(MASConstraintMaker *make) {

      

            make.top.mas_equalTo(10 *AUTO_HEIGHT);

            make.left.mas_equalTo(addImageV.mas_right).offset(7 *AUTO_WIDTH);

            make.height.mas_equalTo(16 *AUTO_HEIGHT);

        make.width.mas_equalTo(1);


    }];

7.添加    UISearchBar  UISearchBar添加在searchView右边


    self.mySearchBar = [UISearchBar new];

    [searchView addSubview:self.mySearchBar];

    [self.mySearchBar mas_makeConstraints:^(MASConstraintMaker *make) {

       make.top.mas_equalTo(_addressL);

       make.left.mas_equalTo(addImageV.mas_right).offset(12 * AUTO_WIDTH);

        make.height.mas_equalTo(40 * AUTO_HEIGHT);

       make.width.mas_equalTo(210 * AUTO_WIDTH);

        

    }];

    self.mySearchBar.placeholder = @"地址";

    [self.mySearchBar setShowsCancelButton:NO];

    self.mySearchBar.keyboardType = UIKeyboardTypeNamePhonePad;

    [self.mySearchBar setContentMode:UIViewContentModeLeft];

    

    self.mySearchBar.delegate = self;



8.  //去掉搜索框背景

  [[[self.mySearchBar.subviews objectAtIndex:0].subviews objectAtIndex:0]removeFromSuperview];

删除view

    UIView *view =   [[self.mySearchBar.subviewsobjectAtIndex:0].subviewsobjectAtIndex:0];

    [view removeFromSuperview];

9.把UITextField添加到self.mySearchBar中

    UITextField *text = [UITextFieldnew];

    //  [self.mySearchBar addSubview:text];

    text.backgroundColor =[UIColorclearColor];

    text.returnKeyType =UIReturnKeySearch;

    [self.mySearchBarinsertSubview:text atIndex:0];

    [text mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.mas_equalTo(-8 * AUTO_HEIGHT);

        make.left.mas_equalTo(8 *AUTO_WIDTH);

        make.height.mas_equalTo(40 *AUTO_HEIGHT);

        make.right.mas_equalTo(self.mySearchBar.mas_right).offset(0 * AUTO_WIDTH);

 

        

    }];

    //设置UITextField是否拥有一键清除的功能

    text.clearsOnBeginEditing=YES;


    //设置一键清除按钮是否出现

    text.clearButtonMode=UITextFieldViewModeNever;

    text.tintColor = GH;

    text.placeholder = @"职位,姓名";

    text.textColor = [UIColorwhiteColor];

    text.font = [UIFontfontWithName:@"Arial"size:TEXTCONTENTFONT2 *AUTO_WIDTH];

    text.clearButtonMode =UITextFieldViewModeWhileEditing;

    text.delegate = self;

    




    







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值