简体   繁体   English

搜索栏中的iOS更改图标

[英]iOS Change icon in the search bar

I'm trying to customize a search bar. 我正在尝试自定义搜索栏。 I've figured out how to change the background and the text box within the search bar, but I can't find a way to change the icon that is default in the bar (the magnify glass icon). 我已经想出如何更改搜索栏中的背景和文本框,但我找不到更改栏中默认图标的方法(放大玻璃图标)。

Is there a way to do this? 有没有办法做到这一点?

Open-source is your friend (just like cocoa is, huh): 开源是你的朋友(就像可可一样,呵呵):

- (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];
  }  
}

Since iOS 5.0: 自iOS 5.0起:

[self.testSearchBar setImage:[UIImage imageNamed: @"favicon.png"]
            forSearchBarIcon:UISearchBarIconSearch
                       state:UIControlStateNormal];
searchBar.setImage(UIImage(named: "tempUser")!,
                forSearchBarIcon: UISearchBarIcon.Search, 
                           state: UIControlState.Normal)

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

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