iOS自带的UISearchBar有很多限制,我们可以使用UITextField做出一个类似于SearchBar的效果。

//=================================================
    //                 自定义SearchBar
    //=================================================
    
    // 1.创建一个UITextField作为背景
    UITextField *searchBar = [[UITextField alloc] init];
    searchBar.width = 420;
    searchBar.height = 30;
    searchBar.font = [UIFont systemFontOfSize:14];
    searchBar.background = [UIImage imageNamed:@"searchbar_textfield_background"];

    // 2.添加左侧的小图标
//    UIImage *searchIcon = [UIImage imageNamed:@"searchbar_textfield_search_icon"];
//    UIImageView *searchIconView = [[UIImageView alloc] initWithImage:searchIcon]; // 使用此种形式创建出来的ImageView是有默认大小的。
    
    UIImageView *searchIconView = [[UIImageView alloc] init];
    searchIconView.image = [UIImage imageNamed:@"searchbar_textfield_search_icon"];
    searchIconView.width = 30;
    searchIconView.height = 30;
    
    searchIconView.contentMode = UIViewContentModeCenter;
    searchBar.leftView = searchIconView;
    searchBar.leftViewMode = UITextFieldViewModeAlways;
    
    self.navigationItem.titleView = searchBar;

最终效果:

新浪微博客户端(5)-自定义UISearchBar

 

相关文章:

  • 2022-12-23
  • 2021-08-13
  • 2022-12-23
  • 2021-07-24
  • 2021-09-07
  • 2021-10-21
  • 2021-06-24
  • 2021-11-27
猜你喜欢
  • 2021-09-10
  • 2021-05-23
  • 2021-08-22
  • 2021-04-02
  • 2021-04-29
  • 2022-03-03
相关资源
相似解决方案