objective-c - 如何在 ios8 中更改 UISearchBar 文本字段背景颜色和文本颜色

标签 objective-c ios8

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

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

但是对我不起作用,谁能给出解决方案。 提前致谢

最佳答案

试试这个:

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];

关于objective-c - 如何在 ios8 中更改 UISearchBar 文本字段背景颜色和文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32968907/

相关文章:

ios - UIView 图标动画

ios - 在 Xcode 的编译时禁用 -ffast-math

ios - 在 App Delegate 级别基于设备强制定向并禁用旋转?

ios - 确定是否设置了对照片库的访问 - PHPhotoLibrary

ios - 如何测试 SKPaymentTransactionStateDeferred?

iphone - 如果 NSDictionary 键顺序从不改变,是否保证与初始化的顺序相同?

objective-c - 类型 'Boolean' 不符合协议(protocol) 'BooleanType'

ios - 如何不断检查用户是否接近坐标列表中的某个坐标? (为了获得最佳性能)

ios - 在 IOS 中处理使用数据库登录的 TouchID 身份验证

ios - 在 iOS 8 自动高度的单元格内键入时,UITableView 在 endUpdates 上跳转到顶部