ios 导航栏添加搜索框


```

#pragma mark - 搜索栏

    //添加方法一  (此方法当界面消失时,需要在viewWillDisappear方法中remove掉UISearchBar……所以建议使用第二种添加方法)

    //CGRect mainViewBounds = self.navigationController.view.bounds;

    //self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(CGRectGetWidth(mainViewBounds)/2-((CGRectGetWidth(mainViewBounds)-120)/2), CGRectGetMinY(mainViewBounds)+28, CGRectGetWidth(mainViewBounds)-120, 30)];

    //添加到view中

    //[self.navigationController.view addSubview:self.searchBar];


    //添加方法二

    UIView*titleView = [[UIViewalloc]initWithFrame:CGRectMake(0,0,ScreenW-100,30)];

    //UIColor *color =  self.navigationController.navigationBar.tintColor;

    //[titleView setBackgroundColor:color];


    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, ScreenW - 100, 30)];



    self.searchBar.placeholder = @"请输入流水号、商品信息或会员信息";

    self.searchBar.layer.cornerRadius = 15;

    self.searchBar.layer.masksToBounds = YES;

    //设置背景图是为了去掉上下黑线

    self.searchBar.backgroundImage = [[UIImage alloc] init];

    //self.searchBar.backgroundImage = [UIImage imageNamed:@"sexBankgroundImage"];

    // 设置SearchBar的主题颜色

    //self.searchBar.barTintColor = [UIColor colorWithRed:111 green:212 blue:163 alpha:1];

    //设置背景色

    self.searchBar.backgroundColor = [UIColor colorWithRed:0/255.0f green:0/255.0f blue:0/255.0f alpha:0.1];

    // 修改cancel

    self.searchBar.showsCancelButton=NO;

    self.searchBar.barStyle=UIBarStyleDefault;

    self.searchBar.keyboardType=UIKeyboardTypeNamePhonePad;

    //self.searchBar.searchBarStyle = UISearchBarStyleMinimal;//没有背影,透明样式

    self.searchBar.delegate=self;


    // 修改cancel

    self.searchBar.showsSearchResultsButton=NO;

    //5. 设置搜索Icon

    [self.searchBar setImage:[UIImage imageNamed:@"Search_Icon"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];



    /*这段代码有个特别的地方就是通过KVC获得到UISearchBar的私有变量

     searchField(类型为UITextField),设置SearchBar的边框颜色和圆角实际上也就变成了设置searchField的边框颜色和圆角,你可以试试直接设置SearchBar.layer.borderColor和cornerRadius,会发现这样做是有问题的。*/

    //一下代码为修改placeholder字体的颜色和大小

    UITextField*searchField = [_searchBarvalueForKey:@"_searchField"];

    //2. 设置圆角和边框颜色

    if(searchField) {

        [searchFieldsetBackgroundColor:[UIColor clearColor]];

        //        searchField.layer.borderColor = [UIColor colorWithRed:49/255.0f green:193/255.0f blue:123/255.0f alpha:1].CGColor;

        //        searchField.layer.borderWidth = 1;

        //        searchField.layer.cornerRadius = 23.0f;

        //        searchField.layer.masksToBounds = YES;

        // 根据@"_placeholderLabel.textColor" 找到placeholder的字体颜色

        [searchFieldsetValue:[UIColor colorWithRed:200/255.0f green:200/255.0f blue:200/255.0f alpha:1] forKeyPath:@"_placeholderLabel.textColor"];

    }

    // 输入文本颜色

    searchField.textColor= [UIColorwhiteColor];

    searchField.font= [UIFontsystemFontOfSize:15];

    // 默认文本大小

    [searchFieldsetValue:[UIFont boldSystemFontOfSize:13] forKeyPath:@"_placeholderLabel.font"];

    //只有编辑时出现出现那个叉叉

    searchField.clearButtonMode = UITextFieldViewModeWhileEditing;


    [titleViewaddSubview:self.searchBar];

    //Set to titleView

    self.navigationItem.titleView = titleView;


```

你可能感兴趣的:(ios 导航栏添加搜索框)