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;

初始状态


进入搜索状态

当设置成UIBarPositionBottom时,如图


初始状态


进入搜索状态

当设置成UIBarPositionTop时,如图


初始状态


进入搜索状态

当设置成UIBarPositionTopAttached时,如图


初始状态


进入搜索状态


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

self.searchBar= [[UISearchBaralloc]init];

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

_searchBar.delegate=self;

_searchBar.placeholder=@"搜索";

self.navigationItem.titleView=self.searchBar;


初始状态


进入搜索状态

#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


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

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

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前言 在iOS8中,Apple在UIKit框架中给我们提供了UISearchController来代替之前的UIS...
    SeraZheng阅读 3,355评论 12 23
  • 最近,在项目过程中遇到要自定义SearchBar的外观,虽然自己觉得用系统默认的外观就行了,不过UI设计师要求不用...
    lexiaoyao20阅读 26,343评论 89 208
  • 作者唯一QQ:228544117。。。。。 =========后面的都要新建一个文章 AppDelegate.h ...
    CC_iOS阅读 1,003评论 0 0
  • 1、searchBar 本例子实现布局:上面是一个navigationController,接下来一个search...
    lilinjianshu阅读 3,500评论 1 8
  • #import<Foundation/Foundation.h> #import #import "sqlTest...
    lichengjin阅读 436评论 0 0