iOS 更改搜索栏中的图标

标签 ios uikit icons uisearchbar

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

有办法吗?

最佳答案

Open-source是你的 friend (就像 cocoa 一样,呵呵):

- (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 更改搜索栏中的图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9794552/

相关文章:

ios - 创建一个自定义分段控件,例如

android - 使用 wifi 验证智能手机(iOS、Android)

iphone - 自定义 UITableView 重新排序

icons - 如何为红色程序中的 View 设置图标?

objective-c - UITableView/UITabBarController : trying to figure out a method to pass a variable to my destination controller when a row is selected

ios - NSURLProtocol 视频请求失败

ios - 使用另一行时模糊 UIPickerView 行之一

iphone - 针规的 UIControl 又名 Google O Meter

html - 社交媒体图标的位置在页面中不一致

delphi - 如何在运行时将 48x48 .ico 加载到图像列表中并保持透明度