如何在ios8中更改UISearchBar文本字段背景颜色和文本颜色 [英] how to change UISearchBar textfield background color and text color in ios8

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

问题描述

我使用下面的代码来更改UISearchBar文本字段背景颜色。

I used below code for changing UISearchBar textfield background color.

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

但它对我不起作用,任何人都可以提供解决方案。
提前感谢

but it does not work for me, can any one give solution. thanks in advance

推荐答案

试试这个:

UITextField *searchField = [self.searchBar valueForKey:@"searchField"];

// To change background color
searchField.backgroundColor = [UIColor blueColor];

// To change text color
searchField.textColor = [UIColor redColor];

// To change placeholder text color
searchField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Some Text"];
UILabel *placeholderLabel = [searchField valueForKey:@"placeholderLabel"];
placeholderLabel.textColor = [UIColor grayColor];

这篇关于如何在ios8中更改UISearchBar文本字段背景颜色和文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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