如何在iOS7中更改UISearchBar的取消按钮的textColor? [英] How to change textColor of Cancel button of UISearchBar in iOS7?

查看:116
本文介绍了如何在iOS7中更改UISearchBar的取消按钮的textColor?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在iOS7中更改 UISearchBar 的取消按钮文字的颜色。

I need to change the color of Cancel button text of UISearchBar in iOS7.

通常 UISearchBar 取消按钮 textColor 是蓝色,我想将 textColor 更改为 redColor

Normally UISearchBar Cancel button textColor is blue and I want to change textColor to redColor.

如何更改?

推荐答案

我找到了自己问题的答案。

I found answers for my own questions.

这是代码,加上 AppDelegate 如果你想要更改所有取消按钮。

Here is code , add in AppDelegate if you want to change all cancel button.

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

Swift:

let attributes = [NSForegroundColorAttributeName : UIColor.red]
    UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: .normal)

这篇关于如何在iOS7中更改UISearchBar的取消按钮的textColor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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