美文网首页
UISearchBar样式修改

UISearchBar样式修改

作者: 超_iOS | 来源:发表于2017-12-04 14:08 被阅读35次
    -(UISearchBar *)searchBar{
        if (!_searchBar) {
            _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(8, 27, HOME_SCREEN_WIDTH - 64.0, 30.0)];
            _searchBar.barStyle = UIBarStyleDefault;
            _searchBar.placeholder = @"ID";
            [self setSearchBarBgColor:_searchBar];
            _searchBar.delegate = self;
            _searchBar.tintColor = [UIColor blackColor];//光标颜色
            //_searchBar.barTintColor = [UIColor redColor];//背景颜色
         
            [_searchBar setImage:[UIImage imageNamed:@"关闭搜索"] forSearchBarIcon:UISearchBarIconClear state:UIControlStateNormal];
             [_searchBar setImage:[UIImage imageNamed:@"关闭搜索按下"] forSearchBarIcon:UISearchBarIconClear state:UIControlStateHighlighted];//清除按钮
        //    [_searchBar setImage:[UIImage imageNamed:@"搜索按钮"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];//搜索按钮
            
            
            UITextField *txfSearchField = [_searchBar valueForKey:@"searchField"];
            txfSearchField.backgroundColor = white_color;// RGB(206, 205, 210);
            txfSearchField.layer.cornerRadius = 30 / 2.0;
            txfSearchField.layer.masksToBounds = YES;
            txfSearchField.leftView = nil;//移除搜索按钮
        }
        return _searchBar;
    }
    
    -(void)setSearchBarBgColor:(UISearchBar *)mSearchBar{
        if ([[_searchBar subviews] count] > 0)
        {
            UIView * thesuperview = [[_searchBar subviews] objectAtIndex:0];
            NSArray *viewSubvies = [thesuperview subviews];
            for (UIView *_theview in viewSubvies)
            {
                if ([_theview isKindOfClass:[UITextField class]])
                {
                }
                if ([_theview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
                {
                    [_theview removeFromSuperview];//去掉背景,否searchbar则会闪一下灰色背景
                }
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:UISearchBar样式修改

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