UISearchBar *searchBar = [UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
for (UIView* subview in [[searchBar.subviews lastObject] subviews]) {
if ([subview isKindOfClass:[UITextField class]]) {UITextField *textField = (UITextField*)subview;
textField.textColor = [UIColor redColor]; //修改输入字体的颜色
[textField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"]; //修改placeholder的颜色
} else if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
{
[subview removeFromSuperview];
}
}

本文详细介绍了如何使用Objective-C代码修改UISearchBar组件中UITextField的字体颜色、背景颜色及placeholder文字颜色,提供了完整的实现步骤。
3221

被折叠的 条评论
为什么被折叠?



