如何更改UISearchBar取消的字体颜色

需要更改UISearchBar中“取消”文本的色值,但UISearchBar又没这属性,于是上网搜,,但网上全他大爷的都是一样的东东,不知道被拷贝粘贴多少次的,我也粘下吧:
    _searchBar.showsCancelButton = YES;
    for(UIView *subView in _searchBar.subviews){
        if([subView isKindOfClass:UIView.class]){
            for (UIView *v in subView.subviews) {
                if([v isKindOfClass:[UIButton class]]){
                    UIButton *bt = (UIButton *)v;
                    [bt setTitle:@"取消" forState:UIControlStateNormal];
                    [bt setTitleColor:COLOR_APP_BLUE forState:UIControlStateNormal];
                }
            }
        }
    }

确实能一次性的实现修改色值的效果,,注意,一次性,,当然如果你想实现,编辑文本的时候显示“取消”,停止编辑时隐藏“取消”,那么你会发现,这个方法很坑爹。火大,,最终终于找到合适的解决方式,代码如下:

[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor],NSForegroundColorAttributeName,nil] forState:UIControlStateNormal];

终于实现了想要的效果。

下面粘个图吧:




猜你喜欢

转载自blog.csdn.net/u011189158/article/details/48754103
今日推荐