iOS-UISerachBar

//修改右側(cè)返回按鈕的語(yǔ)言

-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    [_searchBar setShowsCancelButton:YES animated:YES];
    [self chanageSearchBar:searchBar cancleTitle:@"取消"];
}
-(void)chanageSearchBar:(UISearchBar *)searchBar
            cancleTitle:(NSString *)cancleTitle
{
    for (UIView *view in [searchBar.subviews[0] subviews])  {
        if([view isKindOfClass:[UIButton class]])
        {
            UIButton *button = (UIButton *)view;
            [button setTitle:cancleTitle forState:UIControlStateNormal];
        }
    }
    
    //用block遍歷數(shù)組
    /*
    [[searchBar.subviews[0] subviews] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if([obj isKindOfClass:[UIButton class]])
        {
            [obj setTitle:cancleTitle forState:UIControlStateNormal];
        }
    }];
     */
}

UISearchBar屬性相關(guān)

_searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];// 初始化,不解釋
    [self.searchBar setPlaceholder:@"Search"];// 搜索框的占位符
    [self.searchBar setPrompt:@"Prompt"];// 頂部提示文本,相當(dāng)于控件的Title
    [self.searchBar setBarStyle:UIBarMetricsDefault];// 搜索框樣式
    [self.searchBar setTintColor:[UIColor blackColor]];// 搜索框的顏色,當(dāng)設(shè)置此屬性時(shí),barStyle將失效
    [self.searchBar setTranslucent:YES];// 設(shè)置是否透明
    [self.searchBar setBackgroundImage:[UIImageimageNamed:@"image0"]];// 設(shè)置背景圖片
    [self.searchBar setSearchFieldBackgroundImage:[UIImageimageNamed:@"image3"] forState:UIControlStateNormal];// 設(shè)置搜索框中文本框的背景
    [self.searchBar setSearchFieldBackgroundImage:[UIImageimageNamed:@"image0"] forState:UIControlStateHighlighted];
    [self.searchBar setSearchFieldBackgroundPositionAdjustment:UIOffsetMake(30, 30)];// 設(shè)置搜索框中文本框的背景的偏移量
    
    [self.searchBar setSearchResultsButtonSelected:NO];// 設(shè)置搜索結(jié)果按鈕是否選中
    [self.searchBar setShowsSearchResultsButton:YES];// 是否顯示搜索結(jié)果按鈕
    
    [self.searchBar setSearchTextPositionAdjustment:UIOffsetMake(30,0)];// 設(shè)置搜索框中文本框的文本偏移量
   
    [self.searchBar setInputAccessoryView:_btnHide];// 提供一個(gè)遮蓋視圖
    [self.searchBar setKeyboardType:UIKeyboardTypeEmailAddress];// 設(shè)置鍵盤樣式
    
    // 設(shè)置搜索框下邊的分欄條
    [self.searchBar setShowsScopeBar:YES];// 是否顯示分欄條
    [self.searchBar setScopeButtonTitles:[NSArrayarrayWithObjects:@"Singer",@"Song",@"Album", nil]];// 分欄條,欄目
    [self.searchBar setScopeBarBackgroundImage:[UIImageimageNamed:@"image3"]];// 分欄條的背景顏色
    [self.searchBar setSelectedScopeButtonIndex:1];// 分欄條默認(rèn)選中的按鈕的下標(biāo)
    
    
    [self.searchBar setShowsBookmarkButton:YES];// 是否顯示右側(cè)的“書圖標(biāo)”
    
    [self.searchBar setShowsCancelButton:YES];// 是否顯示取消按鈕
    [self.searchBar setShowsCancelButton:YES animated:YES];
// 是否提供自動(dòng)修正功能(這個(gè)方法一般都不用的)
    [self.searchBar setSpellCheckingType:UITextSpellCheckingTypeYes];// 設(shè)置自動(dòng)檢查的類型
    [self.searchBar setAutocorrectionType:UITextAutocorrectionTypeDefault];// 是否提供自動(dòng)修正功能,一般設(shè)置為UITextAutocorrectionTypeDefault
   
    self.searchBar.delegate = self;// 設(shè)置代理
    [self.searchBar sizeToFit];
    myTableView.contentInset =UIEdgeInsetsMake(CGRectGetHeight(self.searchBar.bounds), 0, 0, 0);
    [self.view addSubview:myTableView];
  
    [myTableView addSubview:self.searchBar];
最后編輯于
?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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