更改UISearchbar放大镜颜色,PlaceHolder颜色和X颜色

时间:2015-12-26 16:33:06

标签: ios swift uisearchbar uicolor

我已经搜索了各种解决方案来完成这项任务,但它们要么是目标C,要么是更换放大镜图像。

enter image description here

enter image description here

以前的帖子我看了:

Change color of magnifying glass

How to change UISearchBar Placeholder and image tint color?

我不想替换放大镜图像的原因是因为页面颜色是动态的,并且有超过100种颜色组合

任何有关更改UISearchbar放大镜颜色,PlaceHolder颜色和X颜色的帮助都将非常感激

2 个答案:

答案 0 :(得分:11)

目标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()

答案 1 :(得分:1)

Shahil的答案更新为Swift 3:

<ion-content>
    <ion-searchbar
      placeholder="Find"
      [(ngModel)]="myInput">
    </ion-searchbar>
    <button ion-item *ngFor="let item of listItem | my-filter: myInput"">
</ion-content>