溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何修改系統(tǒng)UISearchBar的樣式

發(fā)布時間:2020-07-13 00:04:56 來源:網(wǎng)絡 閱讀:727 作者:一網(wǎng)打盡2 欄目:移動開發(fā)

        自從接觸iOS快一年多的時間了,感覺自己還是菜鳥一枚,最近在整理自己的開發(fā)過程中得點點滴滴,想通過博客的形式記錄下來。本博客會陸續(xù)添加內(nèi)容,鑒于本人水平有限,如有錯誤,請給與指正,感激不盡。

        修改UISearchBar的樣式,網(wǎng)上也有很多種,也都大同小異,本篇也不例外,但確實很實用,廢話不多說,上代碼:

        1.UISearchBar帶有背景顏色的

         if (!_searchBar) {
        _searchBar=[[UISearchBar alloc]initWithFrame:CGRectMake(10, 20, self.view.frame.size.width-20, 40)];
        _searchBar.placeholder=@"搜索";
        _searchBar.delegate=self;
        //searchBar的背景顏色
        //1.
        [_searchBar setBackgroundColor:[UIColor orangeColor]];
        //2.關鍵
        [[[_searchBar.subviews objectAtIndex:0].subviews objectAtIndex:0] removeFromSuperview];
        
        
        //顯示取消按鈕
        _searchBar.showsCancelButton=YES;
        
        UIButton *cancelBtn=[_searchBar valueForKey:@"_cancelButton"];
        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        cancelBtn.titleLabel.font=[UIFont systemFontOfSize:14];
        [cancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        //設置光標的顏色
        _searchBar.tintColor=[UIColor redColor];
        
        
        UITextField *searchFiled=[_searchBar valueForKey:@"_searchField"];
        //設置處于編輯狀態(tài)
        [searchFiled becomeFirstResponder];
        
        //輸入文本的顏色
        searchFiled.textColor=[UIColor greenColor];
        
        //輸入文本字體的大小
        searchFiled.font=[UIFont systemFontOfSize:14];
        
        //輸入框的圓角設置
        searchFiled.layer.cornerRadius=10;
        searchFiled.layer.masksToBounds=YES;
        
        //輸入框里面的背景顏色
        searchFiled.backgroundColor=[UIColor whiteColor];
        
        //提示文本的顏色
        [searchFiled setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
        
        self.cancelField=searchFiled;
        
        //輸入框里的圖標
        [_searchBar setImage:[UIImage p_w_picpathNamed:@"boy.jpg"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];

        [self.view addSubview:_searchBar]       

      2.UISearchBar不帶背景顏色的

       //    如果不要searchBar的背景顏色
//    1.關鍵
    [_searchBar setBackgroundColor:[UIColor clearColor]];
    [[[_searchBar.subviews objectAtIndex:0].subviews objectAtIndex:0] removeFromSuperview];
//    2.
    UIView *searchView=[[UIView alloc]initWithFrame:CGRectMake(10, 20, self.view.frame.size.width-20, 40)];
    searchView.layer.cornerRadius=10;
    searchView.layer.borderColor=[UIColor orangeColor].CGColor;
    searchView.layer.borderWidth=1.f;
    [self.view addSubview:searchView];
    [self.view addSubview:_searchBar];


#pragram--

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    NSLog(@"實時監(jiān)控文字輸入");
}


- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"點擊搜索按鈕");
}


- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"點擊取消按鈕");
    [self.cancelField resignFirstResponder];
}

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI