如何设置环信自带搜索取消按钮的颜色

方法一:修改取消按钮文字颜色及背景图片的代码片段,要放到代理方法中修改,否则遍历找不着呀,那就修改不了了

-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    // 显示取消按钮
    [searchBar setShowsCancelButton:YES animated:YES];

    // 修改UISearchBar右侧的取消按钮文字颜色及背景图片
    for (UIView *searchbuttons in [searchBar subviews]){
        if ([searchbuttons isKindOfClass:[UIButton class]]) {
            UIButton *cancelButton = (UIButton*)searchbuttons;
            // 修改文字颜色
            [cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
            
            // 修改按钮背景
            [cancelButton setBackgroundImage:[UIImage resizedImage:@"login_btn_login.png"] forState:UIControlStateNormal];
            [cancelButton setBackgroundImage:nil forState:UIControlStateHighlighted];
        }
    }
}

对于用环信的搜索框,然并卵~用方法二轻松解决

方法二:在searchBar的懒加载中设置

- (UISearchBar *)searchBar
{
    if (!_searchBar) {
        _searchBar = [[EMSearchBar alloc] initWithFrame: CGRectMake(0, 0, self.view.frame.size.width, 44)];
        _searchBar.delegate = self;
        _searchBar.placeholder =@"请输入联系人";  
        [UIColor colorWithRed:0.747 green:0.756 blue:0.751 alpha:1.000];
        [[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blueColor],UITextAttributeTextColor,[NSValue valueWithUIOffset:UIOffsetMake(0, 1)],UITextAttributeTextShadowOffset,nil]forState:UIControlStateNormal];
    }
    return _searchBar;
}

你可能感兴趣的:(如何设置环信自带搜索取消按钮的颜色)