继承协议:
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]; } } } } }]; }