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

最佳答案

您可以通过以下方式更改放大图标,使其适应您的需求:-

#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

它对我有用:)

关于iphone - 如何更改 UISearchBar 以将文本字段内的搜索图标替换为文本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10513858/

10-08 23:27