更改文本后无法更改 UISearchBar 取消按钮标题颜色. [英] Unable to change UISearchBar cancel button title color after changing it's text.

查看:25
本文介绍了更改文本后无法更改 UISearchBar 取消按钮标题颜色.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码更改 UISearchBar 取消按钮标题:

I'm using this code to change UISearchBar cancel button title:

-(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller{
self.searchDisplayController.searchBar.showsCancelButton = YES;
UIView* view=_otsinguRiba.subviews[0];
for (UIView *subView in view.subviews) {
    if ([subView isKindOfClass:[UIButton class]]) {
        UIButton *cancelButton = (UIButton*)subView;

        if (cancelButton) {
        [cancelButton setTitle:@"Test") forState:UIControlStateNormal];
        [cancelButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        }
    }
}
}

虽然更改文本可以正常工作,但更改颜色却不行.它保持黑色.

While changing the text works fine, changing the color doesn't. It stays black.

推荐答案

我不久前在 SO 上找到了这个问题的答案,但我不记得在哪里了.这是我用来设置文本颜色的代码.

I found the answer to this on SO a while back, but I don't recall where. Here's the code that I'm using to set the color of the text.

[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                [UIColor redColor],NSForegroundColorAttributeName,
                //[UIColor whiteColor],UITextAttributeTextShadowColor,
                //[NSValue valueWithUIOffset:UIOffsetMake(0, 1)],UITextAttributeTextShadowOffset,
                nil]
        forState:UIControlStateNormal];

这篇关于更改文本后无法更改 UISearchBar 取消按钮标题颜色.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆