美文网首页
设置UISearchBar

设置UISearchBar

作者: Calvin_83d4 | 来源:发表于2017-09-18 16:39 被阅读0次

    更改searchBar的背景颜色及搜索框的颜色

    - (UISearchBar *)searchBar{

    if (!_searchBar) {

    _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 14.5, self.view.frame.size.width, 46.5)];

    _searchBar.searchBarStyle = UISearchBarStyleProminent;

    _searchBar.placeholder = @"xxxxx";

    [_searchBar setBackgroundColor:[UIColor whiteColor]];

    [[[_searchBar.subviews objectAtIndex:0].subviews objectAtIndex:0] removeFromSuperview];

    for (UIView *view in [_searchBar.subviews firstObject].subviews) {

    if ([view isKindOfClass:[UITextField class]]) {

    [(UITextField *)view addTarget:self action:@selector(searchBarSearchAction:) forControlEvents:UIControlEventEditingChanged];

    [(UITextField *)view setBackground:nil];

    view.backgroundColor = COLORRGB(247, 247, 247);

    }

    }

    [_searchBar setShowsCancelButton:YES];

    for (UIView *cancelBT in [_searchBar.subviews lastObject].subviews) {

    //设置取消按钮

    if ([cancelBT isKindOfClass:[UIButton class]]) {

    UIButton *btn = (UIButton *)cancelBT;

    [btn setTitle:@"取消" forState:(UIControlStateNormal)];

    btn.titleLabel.font = [UIFont systemFontOfSize:16];

    btn.enabled = YES;//使其不在搜索状态是也可以更改

    }

    }

    _searchBar.delegate = self;

    }

    return _searchBar;

    }

    相关文章

      网友评论

          本文标题:设置UISearchBar

          本文链接:https://www.haomeiwen.com/subject/jpybsxtx.html