我已经搜索了各种解决方案来完成这项任务,但它们要么是目标C,要么是更换放大镜图像.
以前的帖子我看了看:
改变放大镜的颜色
如何更改UISearchBar占位符和图像色调?
我不想替换放大镜图像的原因是因为页面颜色是动态的,并且有超过100种颜色组合
任何有关更改UISearchbar放大镜颜色,PlaceHolder颜色和X颜色的帮助将非常感激
目标C:
NSArray *searchBarSubViews = [[self.searchBar.subviews objectAtIndex:0] subviews]; for (UIView *view in searchBarSubViews) { if([view isKindOfClass:[UITextField class]]) { UITextField *textField = (UITextField*)view; UIImageView *imgView = (UIImageView*)textField.leftView; imgView.image = [imgView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; imgView.tintColor = [UIColor whiteColor]; UIButton *btnClear = (UIButton*)[textField valueForKey:@"clearButton"]; [btnClear setImage:[btnClear.imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal]; btnClear.tintColor = [UIColor whiteColor]; } } [self.searchBar reloadInputViews];
斯威夫特:
// Text field in search bar. let textField = searchController.searchBar.valueForKey("searchField") as! UITextField let glassIconView = textField.leftView as! UIImageView glassIconView.image = glassIconView.image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate) glassIconView.tintColor = UIColor.whiteColor() let clearButton = textField.valueForKey("clearButton") as! UIButton clearButton.setImage(clearButton.imageView?.image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate), forState: .Normal) clearButton.tintColor = UIColor.whiteColor()