iOS 7 中的 UISearchBar 文本颜色更改 [英] UISearchBar text color change in iOS 7

查看:22
本文介绍了iOS 7 中的 UISearchBar 文本颜色更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何改变iOS 7UISearchBar的文字颜色?

How to change text color of UISearchBar in iOS 7?

iOS 6 中,我对 UISearchBar 进行了子类化,并在 layoutSubviews 中自定义了 UITextField 子视图的属性UISearchBar.

In iOS 6, I was subclassing the UISearchBar and in layoutSubviews customising the properties of UITextField subview of UISearchBar.

但是在 iOS 7 中,UISearchBar 没有 UITextField 作为它的子视图.如何解决这个问题?

But in iOS 7, UISearchBar doesn't have UITextField as its subview. How to fix this?

推荐答案

在 iOS 7 中要访问 Text Field,您必须在级别上重申更多.像这样更改您的代码

In iOS 7 to access Text Field you have to reiterate on level more. Change your code like this

for (UIView *subView in self.searchBar.subviews)
{
    for (UIView *secondLevelSubview in subView.subviews){
        if ([secondLevelSubview isKindOfClass:[UITextField class]])
        {
            UITextField *searchBarTextField = (UITextField *)secondLevelSubview;

            //set font color here
            searchBarTextField.textColor = [UIColor blackColor];

            break;
        }
    }
}

注意:这不是公共 API

您可以使用 UIControls 的 appearance 属性,例如

You can use appearance Property of UIControls, Like

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]}];

注意:外观代理可用于iOS 9.0+输出

Note: Appearance proxy can be used for iOS 9.0+ OutPut

您可以将 tintcolor 设置为应用于 搜索栏 中的关键元素.

You can set The tintcolor to apply to key elements in the search bar.

使用 tintColor 为前景元素着色.

Use tintColor to tint foreground elements.

使用 barTintColor 为条形背景着色.

Use barTintColor to tint the bar background.

在 iOS v7.0 中,UIView 的所有子类都从基类派生出它们对 tintColor 的行为.有关更多信息,请参阅 UIView 级别的 tintColor 讨论.苹果文件

In iOS v7.0, all subclasses of UIView derive their behavior for tintColor from the base class. See the discussion of tintColor at the UIView level for more information. Apple Doc

这篇关于iOS 7 中的 UISearchBar 文本颜色更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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