浏览了文档,我找不到任何可以改变 UISearchBar 颜色的东西。有谁知道如何改变它?没有任何 textColor 属性:/
谢谢
浏览了文档,我找不到任何可以改变 UISearchBar 颜色的东西。有谁知道如何改变它?没有任何 textColor 属性:/
谢谢
适用于 iOS 7 及更高版本:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:@{
NSForegroundColorAttributeName : [UIColor whiteColor],
NSFontAttributeName : [UIFont systemFontOfSize:15]
}];
您也可以删除未使用的属性。
更新。由于appearanceWhenContainedIn
在 iOS 9 中已弃用,请参阅以下 Dishant 的答案:https ://stackoverflow.com/a/38893352/2799722
iOS 8:见https://stackoverflow.com/a/28183058/308315
iOS 6 / 7:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor redColor]];
您可以执行以下操作: 只需从 SearchBar 获取 searchField 属性,然后更改其 textColor 属性。
UITextField *searchField = [searchbar valueForKey:@"_searchField"];
searchField.textColor = [UIColor redColor]; //You can put any color here.
而已!现在您可以以任何可能的方式操作 textField。
这是一个添加此功能的类别:
@implementation UISearchBar (UISearchBar_TextColor)
- (UITextField *)field {
// HACK: This may not work in future iOS versions
for (UIView *subview in self.subviews) {
if ([subview isKindOfClass:[UITextField class]]) {
return (UITextField *)subview;
}
}
return nil;
}
- (UIColor *)textColor {
return self.field.textColor;
}
- (void)setTextColor:(UIColor *)color {
self.field.textColor = color;
}
@end
我怀疑你可以使用这篇文章中描述的技术
稍微修改那里显示的代码,您将 UISearchBar 子类化:
@interface SearchBar : UISearchBar {
}
@end
然后在您的实现中:
- (void)layoutSubviews {
UITextField *searchField;
NSUInteger numViews = [self.subviews count];
for(int i = 0; i < numViews; i++) {
if([[self.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) {
searchField = [self.subviews objectAtIndex:i];
}
}
if(!(searchField == nil)) {
searchField.textColor = [UIColor redColor];
}
[super layoutSubviews];
}
我尚未测试原始帖子的代码或此代码,但看起来它应该可以工作。-wkw
对于 iOS11,我发现这很有效:
将 设置searchController
为后navigationItem
,搜索文本为黑底黑字。为了使它变白,我必须这样做:
searchController.searchBar.barStyle = .blackTranslucent
这是唯一对我有用的东西。我的应用程序有一个透明的导航栏,可以让背景渐变显示出来,我猜 SearchBar 会呈现这种外观,因为我的外观设置UISearchBar
在很大程度上被忽略了,但有一个例外:
UISearchBar.appearance().tintColor = UIColor.red
这使取消按钮和文本插入光标变为红色。占位符文本为浅灰色。
请注意:UISearchBar.appearance().barStyle = .blackTranslucent
不起作用 - 它必须在实例上设置。这对搜索栏也没有明显的影响(它仍然像导航栏一样透明);它只是使搜索文本变白。
AppearanceWhenContainedIn在 iOS 9 中已被弃用,因此我们必须对 iOS 9 及更高版本使用以下方法。
[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]]
setTintColor:[UIColor whiteColor]];
在 iOS 11 中,搜索栏有望成为导航栏的一部分,正如您所料,它会添加各种新的“功能”。
我认为这是一个错误,但我发现我需要执行以下操作来更改文本(和取消按钮)颜色:
self.searchController.searchBar.barStyle = UISearchBarStyleMinimal;
[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]]
setTintColor:[UIColor whiteColor]];
我发现,当保留为“默认”时,条形样式会使文本变黑,无论色调如何等。当设置为“最小”或“突出”时,文本是可见的。
修改了 David Foster (@david-foster) 建议的类别以适用于 iOS 8。
static UITextField *PBFindTextFieldInView(UIView *view) {
for(UIView *subview in view.subviews) {
if([subview isKindOfClass:UITextField.class]) {
return (UITextField *)subview;
} else {
UITextField* textField = PBFindTextFieldInView(subview);
if(textField) {
return textField;
}
}
}
return nil;
}
@implementation UISearchBar (Appearance)
- (UITextField *)field {
return PBFindTextFieldInView(self);
}
- (UIColor *)textColor {
return self.field.textColor;
}
- (void)setTextColor:(UIColor *)color {
self.field.textColor = color;
}
@end
这是一种更清洁的方法:
UITextField *searchField = nil;
for (UIView *v in self.searchBar.subviews)
{
if ([v isKindOfClass:[UITextField class]])
{
searchField = (UITextField *)v;
break;
}
}
if (searchField)
{
searchField.textColor = [UIColor whiteColor];
}
searchController
在为 iOS 11设置后navigationItem
,我发现尝试为 a 中的任何设置via没有textColor
任何影响,但是简单地称为常规的自定义外观属性工作得很好。UIAppearance
UITextField
UISearchBar
textColor
// Implement a custom appearance property via a UITextField category
@interface UITextField (SearchBarTextColor)
@property (nonatomic, strong) UIColor * textColorWorkaround UI_APPEARANCE_SELECTOR;
@end
@implementation UITextField (SearchBarTextColor)
- (UIColor *)textColorWorkaround {
return self.textColor;
}
- (void)setTextColorWorkaround:(UIColor *)textColor {
self.textColor = textColor;
}
@end
然后按如下方式使用:
UITextField *textFieldProxy = [UITextField appearanceWhenContainedInInstancesOfClasses:@[UISearchBar.class]];
textFieldProxy.textColorWorkaround = UIColor.lightGrayColor;
PS同样的技巧帮助我为看似无法访问的标签着色UIDatePicker
和UIPickerView