更改 UISearchBar 的文本颜色和边框颜色 [英] Change text color and border color of a UISearchBar

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

问题描述

我有一个 UISearchBar,搜索样式为minimal",我想更改文本颜色并使用 1px 宽度 的白色边框.我正在尝试 tintcolor 但我什么也没得到

I have an UISearchBar, with search style "minimal", and I want to change the text color and to have a white border with 1px width. I'm trying with tintcolor but I don't get nothing

谢谢.

推荐答案

要更改 UISearchBar 的文本颜色,请使用此代码段

To change the text color of the UISearchBar, use this snippet

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor yourColor]];

对于白色边框,试试这个

For the white border, try this out

for (id object in [[[yourSearchBar subviews] objectAtIndex:0] subviews])
{
    if ([object isKindOfClass:[UITextField class]])
    {
        UITextField *textFieldObject = (UITextField *)object;

        textFieldObject.textColor = [UIColor redColor];
        textFieldObject.layer.borderColor = [[UIColor whiteColor] CGColor];
        textFieldObject.layer.borderWidth = 1.0;
        break;
    }
}

希望这有帮助!

编辑

您可以在应用的任何位置为搜索栏颜色添加第一行.另外,我在循环中为 textColor 添加了一行,检查一下.

You can add the first line for the search bar color anywhere in your app. Also, I have added one line for the textColor in the loop, check that out.

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

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