UISearchBar外观

self.searchController= [[UISearchController alloc] initWithSearchResultsController: nil];

[self.searchController.searchBar sizeToFit];

self.searchController.searchBar.backgroundImage= [UIImage createBgWithColor:[UIColor cyanColor]];

[self.searchController.searchBar setBackgroundImage:[UIImage createBgWithColor:[UIColor greenColor]] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];

self.searchController.searchBar.barTintColor = UIColorHex(f4f4f4);

self.searchController.delegate = self;

self.searchController.searchResultsUpdater = self;

self.searchController.dimsBackgroundDuringPresentation=NO;

self.definesPresentationContext=YES;

self.contactsTableView.tableHeaderView=self.searchController.searchBar;

UISearchBar外观_第1张图片
初始状态


UISearchBar外观_第2张图片
进入搜索状态

当设置成UIBarPositionBottom时,如图


UISearchBar外观_第3张图片
初始状态


UISearchBar外观_第4张图片
进入搜索状态

当设置成UIBarPositionTop时,如图


UISearchBar外观_第5张图片
初始状态


UISearchBar外观_第6张图片
进入搜索状态

当设置成UIBarPositionTopAttached时,如图


UISearchBar外观_第7张图片
初始状态


UISearchBar外观_第8张图片
进入搜索状态


--------------------------------------------------------------------------------------------------------------------------------------

self.searchBar= [[UISearchBaralloc]init];

_searchBar.tintColor=[UIColorblueColor];//解决光标不显示问题

_searchBar.delegate=self;

_searchBar.placeholder=@"搜索";

self.navigationItem.titleView=self.searchBar;


UISearchBar外观_第9张图片
初始状态


UISearchBar外观_第10张图片
进入搜索状态

#pragma mark - UISearchBarDelegate Methods

- (BOOL)searchBarShouldBeginEditing:(UISearchBar*)searchBar{

NSLog(@"11--searchBarShouldBeginEditing");

returnYES;

}// return NO to not become first responder

- (void)searchBarTextDidBeginEditing:(UISearchBar*)searchBar{

NSLog(@"22--searchBarTextDidBeginEditing");

}// called when text starts editing

- (BOOL)searchBar:(UISearchBar*)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text {

NSLog(@"33--shouldChangeTextInRange");

returnYES;

}// called before text changes

- (void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)searchText{

NSLog(@"44--textDidChange");

}// called when text changes (including clear)

- (BOOL)searchBarShouldEndEditing:(UISearchBar*)searchBar{

NSLog(@"是否允许取消搜索--searchBarShouldEndEditing:%s",__func__);

returnYES;

}// return NO to not resign first responder

- (void)searchBarTextDidEndEditing:(UISearchBar*)searchBar{

NSLog(@"结束搜索searchBarTextDidEndEditing:%s",__func__);

}// called when text ends editing

- (void)searchBarSearchButtonClicked:(UISearchBar*)searchBar {

NSLog(@"键盘按钮\"查找\"被点击了searchBarSearchButtonClicked:%s",__func__);

}// called when keyboard search button pressed


- (void)searchBarBookmarkButtonClicked:(UISearchBar*)searchBar {

NSLog(@"键盘按钮\"书签\"被点击了searchBarBookmarkButtonClicked:%s",__func__);

}// called when bookmark button pressed


- (void)searchBarCancelButtonClicked:(UISearchBar*)searchBar {

NSLog(@"键盘按钮\"Cancel\"被点击了searchBarCancelButtonClicked:%s",__func__);

}// called when cancel button pressed


- (void)searchBarResultsListButtonClicked:(UISearchBar*)searchBar {

NSLog(@"键盘按钮\"ResultsList\"被点击了searchBarResultsListButtonClicked:%s",__func__);

}// called when search results button pressed


UISearchBar外观_第11张图片

- (void)searchBar:(UISearchBar*)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope {

NSLog(@"selectedScopeButtonIndexDidChange:%s",__func__);

}//告诉表格,你选择的是那个按钮。

你可能感兴趣的:(UISearchBar外观)