搜索框 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];//設(shè)置是否透明
    [_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.設(shè)置搜索框取消按鈕

  • (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.搜索框響應(yīng)鍵盤(pán) 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.搜索框監(jiān)聽(tīng)輸入內(nèi)容變化

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 作者唯一QQ:228544117。。。。。 =========后面的都要新建一個(gè)文章 AppDelegate.h ...
    CC_iOS閱讀 1,198評(píng)論 0 0
  • 哦吼吼,又研究了幾天,把FMDB這個(gè)封裝好的數(shù)據(jù)庫(kù)搞定了,寫(xiě)了個(gè)簡(jiǎn)單的例子,基于FMDB的添刪改查操作,界面很一般...
    lichengjin閱讀 685評(píng)論 0 0
  • 這個(gè)自定義的控件,在好多地方可以用的到: 所以需要我們創(chuàng)建一個(gè)自定義的控件類(lèi):需要的添加搜索框的時(shí)候,直接調(diào)用自定...
    劉戦軍閱讀 1,318評(píng)論 0 4
  • JavaScript移動(dòng)端模擬微信搜索框searchBar 效果展示: 初始狀態(tài),提示語(yǔ)居中,內(nèi)容:icon+搜索...
    Rocky_Wong閱讀 1,799評(píng)論 0 5
  • UISearchBar屬性相關(guān) _searchBar = [[UISearchBar alloc] initWit...
    DVWang閱讀 646評(píng)論 0 0

友情鏈接更多精彩內(nèi)容