iOS UISearchBar 设置取消按钮,回收键盘,并修改cancel为“取消”

继承协议:

UISearchBarDelegate

在代理方法中设置:

#pragma mark --- 搜索框开始编辑 ---
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    [UIView animateWithDuration:0.3 animations:^{
        //        self.navigationController.navigationBarHidden = YES;
        //        _searchBar.frame = CGRectMake(0, 20, kScreenWidth, 44);
        _searchBar.showsCancelButton = YES;
        
        for (id obj in [searchBar subviews]) {
            if ([obj isKindOfClass:[UIView class]]) {
                for (id obj2 in [obj subviews]) {
                    if ([obj2 isKindOfClass:[UIButton class]]) {
                        UIButton *btn = (UIButton *)obj2;
                        [btn setTitle:@"取消" forState:UIControlStateNormal];
                        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                    }
                }
            }
        }
        
    }];
}

 

posted on 2016-08-05 15:19  Walking_Jin  阅读(5017)  评论(0编辑  收藏  举报

导航