IOS学习笔记——UISearchBar和UISearchDisplayController

来源:互联网 发布:淘宝怎么看店铺等级 编辑:程序博客网 时间:2024/06/10 06:00

随便记录一下开发中遇到的一些问题

  • 关于代理
c    // searchResultsDataSource 就是 UITableViewDataSource    searchDisplayController.searchResultsDataSource = self;    // searchResultsDelegate 就是 UITableViewDelegate    searchDisplayController.searchResultsDelegate = self;

别忘了这个:

csearchDisplayController.delegate = self;
  • 如果想设置 cancelbutton的颜色:
c    searchBar.tintColor = [UIColor whiteColor];
  • 如果想设置 cancelbutton的字体:
    在IOS7下这样设置:
c-(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller{    self.searchDisplayController.searchBar.showsCancelButton = YES;    UIButton *cancelButton;    UIView *topView = self.searchDisplayController.searchBar.subviews[0];    for (UIView *subView in topView.subviews) {        if ([subView isKindOfClass:NSClassFromString(@"UINavigationButton"))]) {            cancelButton = (UIButton*)subView;        }    }    if (cancelButton) {      //Set the new title of the cancel button        [cancelButton setTitle:@"Annuller" forState:UIControlStateNormal];    }}

在IOS5/6下这样设置:

c- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller{   self.searchDisplayController.searchBar.showsCancelButton = YES;   UIButton *cancelButton = nil;   for (UIView *subView in self.searchDisplayController.searchBar.subviews) {      if ([subView isKindOfClass:UIButton)]) {         cancelButton = (UIButton*)subView;      }   }   if (cancelButton){      //Set the new title of the cancel button      [cancelButton setTitle:@"Annuller" forState:UIControlStateNormal];   }}
  • 如果想设置 UISearchBar的背景颜色可以这样设置:
csearchDisplayController.searchBar.barTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"nav_bg"]];
  • 设置UISearchDisplayController是否激活:
    eg:当我点击 搜索列表 的时候,我想让 searchbar恢复原状,即导航栏也恢复原状时;
c[searchDisplayController setActive:NO animated:YES];

tips:改变cancelbutton的颜色

在didFinishLaunchingWithOptions中
把cancelbutton的颜色变成白色的代码如下:

c[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,[NSValue valueWithUIOffset:UIOffsetMake(0, 1)],UITextAttributeTextShadowOffset,nil] forState:UIControlStateNormal];

相关链接:How to change cancel button tint color of UISearchBar in iOS7

0 0
原创粉丝点击