y> 页面出现键盘弹出
感谢//www.greatytc.com/p/742d34f064ee
签协议
@interface NAFirstSearchDetailViewController ()< UISearchControllerDelegate>-
在创建searchController的地方指定代理人
_searchController.delegate = self;
在视图已经出现时设为激活状态, 不能在视图即将出现时
#pragma mark 视图已经出现
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
//第二步
[_searchController setActive:YES];
}
- 实现代理方法, 动画一定要加
#pragma mark UISearchControllerDelegate
//第三步
- (void)didPresentSearchController:(UISearchController *)searchController {
[UIView animateWithDuration:0.1 animations:^{} completion:^(BOOL finished) {
[_searchController.searchBar becomeFirstResponder];
}];
}
- 视图消失要把激活状态设置为NO, 才能回收键盘, 并确保下次进入页面键盘可以弹出, 一定要在即将消失的时候
#pragma mark - 视图即将消失
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
//不取消激活下次再进来就不会弹出键盘
[_searchController setActive:NO];
}
防止pop回来的时候闪一下
感谢 http://blog.csdn.net/x567851326/article/details/51788002
[[[[_searchController.searchBar.subviews objectAtIndex:0] subviews] objectAtIndex:0] removeFromSuperview];
[_searchController.searchBar setBackgroundColor:[UIColor clearColor]];
不想让键盘弹出, 且在点击searchbar的时候跳转到新页面
- 签代理
@interface NAFirstPageViewController ()<UISearchBarDelegate>
- 指定代理人
_searchController.searchBar.delegate = self;
- 实现代理方法
#pragma mark - searchbarDelegate, 开始编辑时不弹出键盘, 并跳转页面
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
[self.navigationController pushViewController:self.searchVc animated:YES];
return NO;
}
修改光标颜色和"取消"按钮颜色
//光标和取消按钮字体颜色
_searchController.searchBar.tintColor = kRGBColor(38, 164, 255);
//"取消"按钮颜色
[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor whiteColor]];
//取消按钮内容
[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTitle:@"取消"];
要是上面方法被废弃了就用这个
[[[_searchController.searchBar.subviews objectAtIndex:0].subviews objectAtIndex:1] setTintColor:kRGBColor(0, 0, 240)];
修改"取消按钮"字体大小
感谢//www.greatytc.com/p/63218245ea76
_searchController.searchBar.showsCancelButton = YES;
_searchController.searchBar.delegate = self;
必须使用searchBarDelegate
#pragma mark - UISearchBarDelegate
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
NSLog(@"start!");
searchBar.showsCancelButton = YES;
NSLog(@"%@",self.searchController.searchBar.subviews[0].subviews);
for (UIView *view in self.searchController.searchBar.subviews[0].subviews) {
if ([view isKindOfClass:[UIButton class]]) {
UIButton *cancelBtn = (UIButton *)view;
[cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
[cancelBtn setTitleColor:APP_GLOBAL_BUTTON_COLOR forState:UIControlStateNormal];
}
}
}