搜索栏中的iOS更改图标 [英] iOS Change icon in the search bar

查看:139
本文介绍了搜索栏中的iOS更改图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自定义搜索栏。我已经想出如何更改搜索栏中的背景和文本框,但我找不到更改栏中默认图标的方法(放大玻璃图标)。

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 是你的朋友(就像可可一样,呵呵):

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

自iOS 5.0起:

[self.testSearchBar setImage:[UIImage imageNamed: @"favicon.png"]
            forSearchBarIcon:UISearchBarIconSearch
                       state:UIControlStateNormal];

这篇关于搜索栏中的iOS更改图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆