y> 頁面出現(xiàn)鍵盤彈出
感謝http://www.reibang.com/p/742d34f064ee
簽協(xié)議
@interface NAFirstSearchDetailViewController ()< UISearchControllerDelegate>-
在創(chuàng)建searchController的地方指定代理人
_searchController.delegate = self;
在視圖已經(jīng)出現(xiàn)時設(shè)為激活狀態(tài), 不能在視圖即將出現(xiàn)時
#pragma mark 視圖已經(jīng)出現(xiàn)
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
//第二步
[_searchController setActive:YES];
}
- 實現(xiàn)代理方法, 動畫一定要加
#pragma mark UISearchControllerDelegate
//第三步
- (void)didPresentSearchController:(UISearchController *)searchController {
[UIView animateWithDuration:0.1 animations:^{} completion:^(BOOL finished) {
[_searchController.searchBar becomeFirstResponder];
}];
}
- 視圖消失要把激活狀態(tài)設(shè)置為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的時候跳轉(zhuǎn)到新頁面
- 簽代理
@interface NAFirstPageViewController ()<UISearchBarDelegate>
- 指定代理人
_searchController.searchBar.delegate = self;
- 實現(xiàn)代理方法
#pragma mark - searchbarDelegate, 開始編輯時不彈出鍵盤, 并跳轉(zhuǎn)頁面
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
[self.navigationController pushViewController:self.searchVc animated:YES];
return NO;
}
修改光標(biāo)顏色和"取消"按鈕顏色
//光標(biāo)和取消按鈕字體顏色
_searchController.searchBar.tintColor = kRGBColor(38, 164, 255);
//"取消"按鈕顏色
[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor whiteColor]];
//取消按鈕內(nèi)容
[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTitle:@"取消"];
要是上面方法被廢棄了就用這個
[[[_searchController.searchBar.subviews objectAtIndex:0].subviews objectAtIndex:1] setTintColor:kRGBColor(0, 0, 240)];
修改"取消按鈕"字體大小
感謝http://www.reibang.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];
}
}
}