IOS学习笔记——UISearchBar和UISearchDisplayController

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

《IOS学习笔记——UISearchBar和UISearchDisplayController》

  • 关于代理
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];

tips:改变文本框的背景

c[self.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"image3"] forState:UIControlStateNormal];

tips:设置搜索框中文本框的背景的偏移量

c[self.searchBar setSearchFieldBackgroundPositionAdjustment:UIOffsetMake(30, 30)];

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

    原文作者:真烦人
    原文地址: https://segmentfault.com/a/1190000000747602
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞