简体   繁体   English

如何更改UISearchBar以使用文本替换文本字段内的搜索图标

[英]how to change the UISearchBar to replace the search icon inside the text field with text

我有UISearchBar,我想删除搜索标记(放大镜图标)并放置文本而不是它

You Can Change the magnify Icon in the following way , adapt it to your needs :- 您可以通过以下方式更改放大图标,使其适应您的需求: -

#import <UIKit/UIKit.h>

@interface SearchBoxExperimentsViewController : UIViewController {  
    IBOutlet UISearchBar *searchBar;
}
@end 

#import "SearchBoxExperimentsViewController.h"
@interface SearchBoxExperimentsViewController (Private)
- (void)setSearchIconToFavicon;
@end

@implementation SearchBoxExperimentsViewController
- (void)viewDidLoad 
{  
    [self setSearchIconToFavicon];  
    [super viewDidLoad];
}

#pragma mark Private
- (void)setSearchIconToFavicon 
{  
    // Really a UISearchBarTextField, but the header is private.  
    UITextField *searchField = nil;  
    for (UIView *subview in searchBar.subviews) {    
        if ([subview isKindOfClass:[UITextField class]]) {      
            searchField = (UITextField *)subview;      
            break;    
        }  
    }    
    if (searchField) {      
        UIImage *image = [UIImage imageNamed: @"favicon.png"];   
        UIImageView *iView = [[UIImageView alloc] initWithImage:image];    
        searchField.leftView = iView;    
        [iView release];  
    }  
}
@end 

It worked for me :) 它对我有用:)

You can use UIAppearance method 您可以使用UIAppearance方法

Swift 3.0 Swift 3.0

UISearchBar.appearance().setImage(UIImage(named: "image_name"), for: .search, state: .normal)

Objective-c Objective-C的

[[UISearchBar appearance] setImage:[UIImage imageNamed:@"image_name"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal]; 

Something a little more approved-API friendly is to use a normal UITextField instead, with the following code to add a label within the UITextField (if you want to remove the magnifying glass, what's your attraction to the search box anyway?): 更友好的API-API更友好的是使用普通的UITextField,使用以下代码在UITextField中添加标签(如果你想删除放大镜,那么你对搜索框的吸引力是什么?):

UILabel *searchFieldLeftLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 32, 15)];
searchFieldLeftLabel.text = @"find:";
searchFieldLeftLabel.placeholder = @"e.g. wings, Boathouse";
searchFieldLeftLabel.font = [UIFont systemFontOfSize:14];
searchFieldLeftLabel.textColor = [UIColor grayColor];
self.searchFieldLeftLabel.leftView = locationSearchFieldLeftLabel;
[searchFieldLeftLabel release];

The result is a nice looking text box that looks like: 结果是一个漂亮的文本框,看起来像:

在此输入图像描述

And when you start typing text, it replaces the placeholder, but the label remains: 当您开始键入文本时,它会替换占位符,但标签仍然是:

在此输入图像描述

See this post Removing the image on the left of an UISearchbar . 请参阅此文章删除UISearchbar左侧的图像 There is no public API to change or remove the magnifier and you should just use a UITextField. 没有公共API可以更改或删除放大镜,您应该只使用UITextField。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM