12 iphone sdk text colors uisearchbar
浏览文档,我找不到任何改变UISearchBar颜色的东西.有人知道如何改变吗?没有任何textColor属性:/
谢谢
mkl*_*kll 11
适用于iOS 7及更高版本:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:@{
NSForegroundColorAttributeName : [UIColor whiteColor],
NSFontAttributeName : [UIFont systemFontOfSize:15]
}];
Run Code Online (Sandbox Code Playgroud)
您也可以删除未使用的属性.
更新.由于appearanceWhenContainedIn在iOS 9中已弃用,请参阅下面的Dishant答案:https://stackoverflow.com/a/38893352/2799722
Geo*_*rts 10
您可以执行以下操作:只需从SearchBar获取searchField属性,然后更改其textColor属性.
UITextField *searchField = [searchbar valueForKey:@"_searchField"];
searchField.textColor = [UIColor redColor]; //You can put any color here.
Run Code Online (Sandbox Code Playgroud)
而已!现在,您可以以任何可能的方式操作textField.
Far*_*our 10
iOS 8:请参阅/sf/answers/1972814091/
iOS 6/7:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor redColor]];
Run Code Online (Sandbox Code Playgroud)
我怀疑你可以使用这篇文章中描述的技术
稍微修改一下那里的代码,您可以对 UISearchBar 进行子类化:
@interface SearchBar : UISearchBar {
}
@end
Run Code Online (Sandbox Code Playgroud)
然后在你的实现中:
- (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];
}
Run Code Online (Sandbox Code Playgroud)
我还没有测试原始帖子的代码或此代码,但看起来它应该可以工作。-wkw
| 归档时间: |
|
| 查看次数: |
11389 次 |
| 最近记录: |