UI基础_UISearchBar

1、UISearchBar的基本属性

// 初始化
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];

    [self.searchBar setPlaceholder:@"Search"];// 搜索框的占位符
    [self.searchBar setPrompt:@"Prompt"];// 顶部提示文本,相当于控件的Title
    [self.searchBar setBarStyle:UIBarMetricsDefault];// 搜索框样式
    [self.searchBar setTintColor:[UIColor blackColor]];// 搜索框的颜色,当设置此属性时,barStyle将失效
    [self.searchBar setTranslucent:YES];// 设置是否透明
    [self.searchBar setBackgroundImage:[UIImage imageNamed:@"image0"]];// 设置背景图片
    [self.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"image3"] forState:UIControlStateNormal];// 设置搜索框中文本框的背景
    [self.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"image0"] forState:UIControlStateHighlighted];
    [self.searchBar setSearchFieldBackgroundPositionAdjustment:UIOffsetMake(30, 30)];// 设置搜索框中文本框的背景的偏移量

    [self.searchBar setSearchResultsButtonSelected:NO];// 设置搜索结果按钮是否选中
    [self.searchBar setShowsSearchResultsButton:YES];// 是否显示搜索结果按钮

    [self.searchBar setSearchTextPositionAdjustment:UIOffsetMake(30, 0)];// 设置搜索框中文本框的文本偏移量

    [self.searchBar setInputAccessoryView:_btnHide];// 提供一个遮盖视图
    [self.searchBar setKeyboardType:UIKeyboardTypeEmailAddress];// 设置键盘样式

    // 设置搜索框下边的分栏条
    [self.searchBar setShowsScopeBar:YES];// 是否显示分栏条
    [self.searchBar setScopeButtonTitles:[NSArray arrayWithObjects:@"Singer",@"Song",@"Album", nil]];// 分栏条,栏目
    [self.searchBar setScopeBarBackgroundImage:[UIImage imageNamed:@"image3"]];// 分栏条的背景颜色
    [self.searchBar setSelectedScopeButtonIndex:1];// 分栏条默认选中的按钮的下标


    [self.searchBar setShowsBookmarkButton:YES];// 是否显示右侧的“书图标”

    [self.searchBar setShowsCancelButton:YES];// 是否显示取消按钮
    [self.searchBar setShowsCancelButton:YES animated:YES];

    // 是否提供自动修正功能(这个方法一般都不用的)
    [self.searchBar setSpellCheckingType:UITextSpellCheckingTypeYes];// 设置自动检查的类型
    [self.searchBar setAutocorrectionType:UITextAutocorrectionTypeDefault];// 是否提供自动修正功能,一般设置为UITextAutocorrectionTypeDefault

    self.searchBar.delegate = self;// 设置代理
    [self.searchBar sizeToFit];
    myTableView.contentInset = UIEdgeInsetsMake(CGRectGetHeight(self.searchBar.bounds), 0, 0, 0);


    [self.view addSubview:myTableView];

    [myTableView addSubview:self.searchBar];

2.代理方法

//搜索框中的内容发生改变时 回调(即要搜索的内容改变)
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{ 
    NSLog(@"changed"); 
    if (_searchBar.text.length == 0) { 
        [self setSearchControllerHidden:YES]; //控制下拉列表的隐现
    }else{ 
        [self setSearchControllerHidden:NO]; 

    } 
} 

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar { 
    searchBar.showsCancelButton = YES; 
for(id cc in [searchBar subviews])
{
if([cc isKindOfClass:[UIButton class]])
{
UIButton *btn = (UIButton *)cc;
[btn setTitle:@"取消" forState:UIControlStateNormal];
}
}
    NSLog(@"shuould begin"); 
    return YES; 
} 

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { 
    searchBar.text = @""; 
    NSLog(@"did begin"); 
} 

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar { 
    NSLog(@"did end"); 
    searchBar.showsCancelButton = NO; 

} 

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { 
    NSLog(@"search clicked"); 
} 

//点击搜索框上的 取消按钮时 调用
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { 
    NSLog(@"cancle clicked"); 
    _searchBar.text = @""; 
    [_searchBar resignFirstResponder]; 
    [self setSearchControllerHidden:YES]; 
} 

3.在UISearchController中设置取消按钮颜色

方法一:

-(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    searchController.searchBar.showsCancelButton = YES;

    UIView * view = [searchController.searchBar.subviews objectAtIndex:0];

    for (UIView *subView in view.subviews) {

        if ([subView isKindOfClass:[UIButton class]]) {

            UIButton *bar = (UIButton *)subView;

            [bar setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

            [bar setTitle:NSLocalizedString(@"取消",nil) forState:UIControlStateNormal];

        }
    }
}

方法二:

[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil, nil]
         setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName,nil]
         forState:UIControlStateNormal];
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值