標籤:ios uisearchbar 修改取消按鈕背景 修改uisearchbar背景
轉載請標明出處:http://blog.csdn.net/android_ls/article/details/39993433
測試的手機IOS系統版本號碼為:6.1.3,實現步驟如下:
1、添加UISearchBar到父View
_searchBar = [[UISearchBar alloc]init]; _searchBar.frame = CGRectMake(0, 0, self.view.frame.size.width, kSeachBarH); _searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth; _searchBar.delegate = self; _searchBar.placeholder = @"請輸入姓名、公司名稱、公司產品名稱"; [self.view addSubview:_searchBar];
2、修改搜尋方塊背景
UIImage *img = [UIImage resizedImage:@"find_bg.png"]; [_searchBar setBackgroundImage:img];
3、修改搜尋輸入框內左側的指示表徵圖
[_searchBar setImage:[UIImage resizedImage:@"ic_search.png"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
4、修改搜尋輸入文本的背景
[_searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"login_btn_input_side.png"] forState:UIControlStateNormal];
註:對於設計人員提供的搜尋輸入文本的背景,若提供的是一個圓角的小方塊,按常理我們會使用展開圖片的中間部分的方法,經測試顯示效果如下:
若讓設計人員重新提供一張固定高度的圖片(比如高是60),當做搜尋輸入文本的背景,如下:
5、修改UISearchBar右側的取消按鈕文字顏色及背景圖片
#pragma mark 搜尋方塊的代理方法,搜尋輸入框獲得焦點(聚焦)-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{ [searchBar setShowsCancelButton:YES animated:YES]; // 修改UISearchBar右側的取消按鈕文字顏色及背景圖片 for (UIView *searchbuttons in [searchBar subviews]){ if ([searchbuttons isKindOfClass:[UIButton class]]) { UIButton *cancelButton = (UIButton*)searchbuttons; // 修改文字顏色 [cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted]; // 修改按鈕背景 [cancelButton setBackgroundImage:[UIImage resizedImage:@"login_btn_login.png"] forState:UIControlStateNormal]; [cancelButton setBackgroundImage:nil forState:UIControlStateHighlighted]; } }}註:修改取消按鈕文字顏色及背景圖片的程式碼片段,一定要放到取消按鈕會顯示代理方法中修改,否則遍曆找不著呀,那就修改不了了。
修改IOS中UISearchBar的取消按鈕背景、搜尋內容輸入文字框背景和UISearchBar的背景