Focus on the UISearchBar but the keyboard not appear

家住魔仙堡 提交于 2019-12-03 11:36:01

I was showing searchbar on textFieldDidBeginEditing:(UITextField *)textField delegate method.

Keyboard was not showing. So for that first resign textField as firstResponder. i.e.

[textField resignFirstResponder];

Then call method with delay

[self performSelector:@selector(callSearchBar) withObject:NULL afterDelay:0.2];

Where

-(void)callSearchBar{
[self.searchDisplayController setActive: YES animated: YES]; 
self.searchDisplayController.searchBar.hidden = NO;
[self.searchDisplayController.searchBar becomeFirstResponder];

}

It works

Use the UISearchBarDelegate and declare the searchBar in the header file like this ...

@interface mySearchScreen : UIViewController <UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate> {

UITableView *myTableView;   
NSMutableArray *tableData;
UISearchBar *sBar;//search bar

and declare your search bar in loadView

- (void)loadView {  
[super loadView];   
sBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0,0,320,30)];   
sBar.delegate = self;   
[self.view addSubview:sBar];    
myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 31, 300, 400)];   
myTableView.delegate = self;    
myTableView.dataSource = self;  
[self.view addSubview:myTableView];

tableData = [[NSMutableArray alloc]init];

}

and then use becomeFirstResponder on your search bar in viewDidAppear

- (void)viewDidAppear:(BOOL)animated {
//ensure the keyboard comes up from the start
[sBar becomeFirstResponder];    
[super viewDidAppear:animated];

}

The sequence matter

[self.searchDisplayController setActive:YES animated:YES];
[self.searchDisplayController.searchBar becomeFirstResponder];

If no luck then check if delegate of the search display controller is linked. Also worth checking the tint colour of the search bar.

On the simulator, make sure that you click on Toggle Software Keyboard to use the keyboard of the simulator as you can see in the image.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!