在修改searchBar上面的placeholder字型顏色時,我自己手寫的代碼跟正確的一模一樣時,它識別不出來,總是崩,錯誤內容說是沒有那個value,真是見鬼了。當我粘貼過來時,它就好了。愛,真是那個什麼了………………
self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(20 , 20, kUISCREEN_WIDTH - 40, 30)]; _searchBar.placeholder = @"個合格"; _searchBar.tintColor = [UIColor whiteColor]; _searchBar.translucent = YES; _searchBar.layer.masksToBounds = YES; _searchBar.layer.cornerRadius = 5.0; _searchBar.alpha = 0.2; //添加背景圖,可以去掉外邊框的灰色部分 [_searchBar setBackgroundImage:[UIImage new]]; [_searchBar setTranslucent:YES]; //這個枚舉可以對searchBar進行修改 _searchBar.searchBarStyle = UISearchBarStyleProminent; //之前的效果,如下面的第一個效果圖 //加上如下命令效果如下 searchBar.barTintColor = [UIColor whiteColor]; //給searchBar中的textField添加背景圖 [_searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"backgroundImage"] forState:UIControlStateNormal]; //一下代碼為修改placeholder字型的顏色和大小 UITextField * searchField = [_searchBar valueForKey:@"_searchField"]; [searchField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"]; [searchField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"]; [topBackImageView addSubview:self.searchBar];
//之前的效果
//之後的效果
其他:
#import "ViewController.h"@interface ViewController ()<UISearchBarDelegate>@property(nonatomic, strong)UISearchBar * searchBar;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(20, 80, 300, 50)]; //預設白色搜尋方塊,多出的背景為灰色;UIBarStyleDefault 預設 UIBarStyleBlack背景為黑色 _searchBar.barStyle = UIBarStyleDefault; //設定搜尋方塊整體的風格為不顯示背景,預設為Prominent顯示 _searchBar.searchBarStyle = UISearchBarStyleDefault; //設定搜尋方塊的文字 _searchBar.text = @"搜尋方塊"; //顯示在searchBar頂部的一行文字// _searchBar.prompt = @"prompt"; //預留位置 _searchBar.placeholder = @"預留位置"; //設定搜尋方塊中的游標的顏色為黃色 _searchBar.tintColor = [UIColor yellowColor]; //設定搜尋方塊的背景顏色 _searchBar.barTintColor = [UIColor redColor]; //設定是否透明 _searchBar.translucent = YES; _searchBar.showsCancelButton = YES; _searchBar.showsBookmarkButton = YES; //設定搜尋方塊textField的位置,其他控制項位置不改變 _searchBar.searchFieldBackgroundPositionAdjustment = UIOffsetMake(50, 0); //設定textField裡面文字在field中的位置 _searchBar.searchTextPositionAdjustment = UIOffsetMake(50, 0); //自訂搜尋方塊放大鏡的表徵圖 [_searchBar setImage:[UIImage imageNamed:@"1"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal]; //設定bookMark表徵圖的設定 [_searchBar setImage:[UIImage imageNamed:@"2"] forSearchBarIcon:UISearchBarIconBookmark state:UIControlStateNormal]; _searchBar.delegate = self; [self.view addSubview:self.searchBar];}#pragma mark - UISearchBarDelegate/*調用BookmarkButton的點擊方法,需要先設定showsBookmarkButton = YES,並且showsSearchResultsButton 不能同時設定為yes,否則不會顯示BookmarkButton,導致無法調用方法//是否在搜尋方塊右側顯示一個圖書的按鈕,預設為NO,_searchBar.showsBookmarkButton = YES;調用ResultsListButton的點擊方法,設定showsSearchResultsButton = YES;//當搜尋方塊將要開始使用時調用。yes表示搜尋方塊可以使用,預設為yes否則搜尋方塊無法使用_searchBar.showsSearchResultsButton = YES; */- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{ NSLog(@"ShouldBegin"); return YES;}//當搜尋方塊開始編輯時候調用- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{ NSLog(@"DidBegin");}//當搜尋方塊將要將要結束使用時調用。- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{ NSLog(@"ShouldEnd"); return YES;}//當搜尋方塊結束編輯時候調用- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{ NSLog(@"DidEnd");}//當field裡面內容改變時候就開始掉用。- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{ NSLog(@"DidChange"); if(searchText != nil && searchText.length > 0){ [self.searchDataAry removeAllObjects]; for (SearchModel * model in self.originAry) { if ([model.shop_name rangeOfString:searchText options:NSCaseInsensitiveSearch].length > 0) { [self.searchDataAry addObject:model]; } } [self.tableView reloadData]; }else{ self.searchDataAry = [NSMutableArray arrayWithArray:self.originAry]; [self.tableView reloadData]; }}//在field裡面輸入時掉用,詢問是否允許輸入,yes表示允許,預設為yes,否則無法輸入- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { NSLog(@"shouldChange"); return YES;}//點擊SearchButton調用- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{ NSLog(@"SearchButtonClicked");}//點擊BookmarkButton調用- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar { NSLog(@"BookmarkButtonClicked");}//點擊CancelButton調用- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { NSLog(@"CancelButton");}//點擊ResultsListButton調用- (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar{ NSLog(@"ResultsListButton");}