我一直在尝试搜索结果几个小时,但我不能得到这个想法.也许是不可能的.我试图改变UISearchBar的占位符文字和放大镜的色调颜色.如果重要的话,我只针对iOS 8.0.这是我的代码,现在看起来如何
let searchBar = UISearchBar() searchBar.placeholder = "Search" searchBar.searchBarStyle = UISearchBarStyle.Minimal searchBar.tintColor = UIColor.whiteColor()
我希望搜索和放大镜是白色的,或者是深绿色.
解决方法
如果您有可以使用的自定义图像,则可以使用类似于以下内容的方式设置图像并更改占位符文本颜色:
[searchBar setImage:[UIImage imageNamed:@"SearchWhite"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal]; UITextField *searchTextField = [searchBar valueForKey:@"_searchField"]; if ([searchTextField respondsToSelector:@selector(setAttributedPlaceholder:)]) { UIColor *color = [UIColor purpleColor]; [searchTextField setAttributedPlaceholder:[[NSAttributedString alloc] initWithString:@"Search" attributes:@{NSForegroundColorAttributeName: color}]]; }
在这个例子中,我使用了purpleColor,而您可以使用(UIColor *)colorWithRed(CGFloat)红色绿色(CGFloat)绿色蓝色(CGFloat)蓝色alpha:(CGFloat)alpha方法来创建自定义深绿色.
编辑:我只是意识到你正在迅速地写这篇文章.快速键入这个,所以我没有把答案放在Obj-C中.
searchBar.setImage(UIImage(named: "SearchWhite"),forSearchBarIcon: UISearchBarIcon.Search,state: UIControlState.Normal); var searchTextField: UITextField? = searchBar.valueForKey("searchField") as? UITextField if searchTextField!.respondsToSelector(Selector("attributedPlaceholder")) { var color = UIColor.purpleColor() let attributeDict = [NSForegroundColorAttributeName: UIColor.purpleColor()] searchTextField!.attributedPlaceholder = NSAttributedString(string: "search",attributes: attributeDict) }