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

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

问题描述

如何在 iOS 7中更改 UISearchBar 的文字颜色

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中访问文本字段,你必须在更多级别重申。更改你的代码

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的属性,比如

You can use appearance Property of UIControls, Like

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

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

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

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

使用 tintColor 为前景元素着色。

使用 barTintColor 为条形背景着色。

在iOS v7.0中,UIView的所有子类从基础派生tintColor的行为类。有关更多信息,请参阅UIView级别的tintColor讨论。
Apple Doc

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天全站免登陆