搜索框 searchbar

1.遵守代理

  • (UISearchBar *)searchBar
    {
    if (_searchBar == nil) {
    _searchBar = [[UISearchBar alloc]init];
    [_searchBar setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHex:0xeaeaea]]];//背景图片
    [_searchBar sizeToFit];
    [_searchBar changeLeftPlaceholder:@"女装"];
    [_searchBar setDelegate:self];
    [_searchBar setKeyboardType:UIKeyboardTypeDefault];
    [_searchBar setTranslucent:YES];//设置是否透明
    [_searchBar setSearchBarStyle:UISearchBarStyleProminent];
    [_searchBar setShowsCancelButton:NO];
    _searchBar.tintColor = [UIColor orangeColor];
    UITextField *txfSearchField = [_searchBar valueForKey:@"_searchField"];
    if (txfSearchField) {
    UIImageView *searchImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
    searchImageView.image = [UIImage imageNamed:@"icon_search"];
    txfSearchField.backgroundColor=[UIColor colorWithHex:0xeaeaea];
    searchImageView.contentMode = UIViewContentModeCenter;
    txfSearchField.leftView = searchImageView;
    txfSearchField.leftViewMode = UITextFieldViewModeAlways;
    }
    }
    [self addSubview:_searchBar];
    [_searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(@7);
    make.left.equalTo(@2);
    make.height.equalTo(@30);
    make.right.equalTo(@-2);
    }];
    return _searchBar;
    }
    3.设置搜索框取消按钮

  • (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
    {
    searchBar.showsCancelButton = YES; //显示“取消”按钮
    for(id cc in [searchBar subviews])
    {
    for (UIView *view in [cc subviews]) {
    if ([NSStringFromClass(view.class)isEqualToString:@"UINavigationButton"])
    {
    UIButton *btn = (UIButton *)view;
    [btn setTitle:@"取消" forState:UIControlStateNormal];
    }
    }
    }
    }
    4.搜索框响应键盘 return事件代理方法

  • (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    NSLog(@"确定事件");
    if (searchBar.text.length) {
    NSLog(@"弹出搜索试图");
    [self finder:searchBar.text];
    }else{
    [self.dateSource removeAllObjects];
    [self updateTabelFrame];
    [self.firendtableview reloadData];
    }
    }
    5.搜索框监听输入内容变化

  • (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    NSLog(@"确定事件");
    if (searchBar.text.length) {
    NSLog(@"弹出搜索试图");
    [self finder:searchBar.text];
    }else{
    [self.dateSource removeAllObjects];
    [self updateTabelFrame];
    [self.firendtableview reloadData];
    }
    }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 作者唯一QQ:228544117。。。。。 =========后面的都要新建一个文章 AppDelegate.h ...
    CC_iOS阅读 1,003评论 0 0
  • 哦吼吼,又研究了几天,把FMDB这个封装好的数据库搞定了,写了个简单的例子,基于FMDB的添删改查操作,界面很一般...
    lichengjin阅读 586评论 0 0
  • 这个自定义的控件,在好多地方可以用的到: 所以需要我们创建一个自定义的控件类:需要的添加搜索框的时候,直接调用自定...
    劉戦軍阅读 1,271评论 0 4
  • JavaScript移动端模拟微信搜索框searchBar 效果展示: 初始状态,提示语居中,内容:icon+搜索...
    Rocky_Wong阅读 1,770评论 0 5
  • UISearchBar属性相关 _searchBar = [[UISearchBar alloc] initWit...
    DVWang阅读 609评论 0 0