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];

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值